blob: 73f6958079ebefc3327ffd44cdb6b4fedac44151 [file] [log] [blame]
/*********************************************************************
* Copyright (c) 2009, 2012 SpringSource, a division of VMware, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
package org.eclipse.virgo.ide.runtime.core.artefacts;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang.ObjectUtils;
import org.eclipse.core.runtime.Assert;
/**
* Safely encapsulates artefact interactions.
*
* @author Miles Parker
*/
public class ArtefactSet implements IArtefactTyped {
private final ArtefactType artefactType;
private final Set<IArtefact> artefacts = new HashSet<IArtefact>();
private final ArtefactRepository repository;
public ArtefactSet(ArtefactRepository repository, ArtefactType artefactType) {
this.repository = repository;
this.artefactType = artefactType;
}
public Iterable<IArtefact> getArtefacts() {
return this.artefacts;
}
public IArtefact[] toArray() {
return this.artefacts.toArray(new IArtefact[] {});
}
public boolean add(IArtefact artefact) {
Assert.isNotNull(artefact, "Tried to add null artefact.");
if (artefact.getArtefactType() == this.artefactType || this.artefactType == ArtefactType.COMBINED) {
artefact.setSet(this);
return this.artefacts.add(artefact);
}
throw new RuntimeException("Tried to add non-matching artefact to " + this.artefactType.name() + ": " + artefact);
}
public ArtefactType getArtefactType() {
return this.artefactType;
}
public ArtefactRepository getRepository() {
return this.repository;
}
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return this.artefactType.getPluralLabel();
}
/**
* Assumes that there is one and only one artefact set of a given type for each set.
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof ArtefactSet) {
ArtefactSet set = (ArtefactSet) obj;
return getArtefactType() == set.getArtefactType() && ObjectUtils.equals(getRepository(), set.getRepository());
}
return super.equals(obj);
}
}