jps model: use JpsElementCollection interface instead of its implementation

This commit is contained in:
nik
2012-10-03 09:43:52 +04:00
parent ffee020f6e
commit d80b67e83e
12 changed files with 45 additions and 25 deletions
@@ -3,11 +3,11 @@ package org.jetbrains.jps.incremental.artifacts.impl;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.jps.incremental.artifacts.JpsBuilderArtifactService;
import org.jetbrains.jps.incremental.artifacts.JpsSyntheticArtifactProvider;
import org.jetbrains.jps.model.JpsElementCollection;
import org.jetbrains.jps.model.JpsModel;
import org.jetbrains.jps.model.artifact.JpsArtifact;
import org.jetbrains.jps.model.artifact.JpsArtifactService;
import org.jetbrains.jps.model.impl.JpsElementChildRoleBase;
import org.jetbrains.jps.model.impl.JpsElementCollectionImpl;
import org.jetbrains.jps.model.impl.JpsElementCollectionRole;
import org.jetbrains.jps.service.JpsServiceManager;
@@ -31,7 +31,7 @@ public class JpsBuilderArtifactServiceImpl extends JpsBuilderArtifactService {
}
public List<JpsArtifact> getSyntheticArtifacts(final JpsModel model) {
JpsElementCollectionImpl<JpsArtifact> artifactsCollection = model.getProject().getContainer().getChild(SYNTHETIC_ARTIFACTS);
JpsElementCollection<JpsArtifact> artifactsCollection = model.getProject().getContainer().getChild(SYNTHETIC_ARTIFACTS);
if (artifactsCollection == null) {
List<JpsArtifact> artifactList = computeSyntheticArtifacts(model);
artifactsCollection = model.getProject().getContainer().setChild(SYNTHETIC_ARTIFACTS);
@@ -16,4 +16,9 @@ public interface JpsElementCollection<E extends JpsElement> extends JpsElement {
<X extends E> X addChild(X element);
void removeChild(@NotNull E element);
void removeAllChildren();
<X extends JpsTypedElement<P>, P extends JpsElement>
Iterable<X> getElementsOfType(@NotNull JpsElementType<P> type);
}
@@ -2,13 +2,13 @@ package org.jetbrains.jps.model.artifact.impl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jps.model.JpsElement;
import org.jetbrains.jps.model.JpsElementCollection;
import org.jetbrains.jps.model.JpsProject;
import org.jetbrains.jps.model.artifact.JpsArtifact;
import org.jetbrains.jps.model.artifact.JpsArtifactReference;
import org.jetbrains.jps.model.artifact.JpsArtifactService;
import org.jetbrains.jps.model.artifact.JpsArtifactType;
import org.jetbrains.jps.model.artifact.elements.JpsCompositePackagingElement;
import org.jetbrains.jps.model.impl.JpsElementCollectionImpl;
import java.util.ArrayList;
import java.util.Collections;
@@ -22,7 +22,7 @@ public class JpsArtifactServiceImpl extends JpsArtifactService {
@Override
public List<JpsArtifact> getArtifacts(@NotNull JpsProject project) {
JpsElementCollectionImpl<JpsArtifact> collection = project.getContainer().getChild(JpsArtifactRole.ARTIFACT_COLLECTION_ROLE);
JpsElementCollection<JpsArtifact> collection = project.getContainer().getChild(JpsArtifactRole.ARTIFACT_COLLECTION_ROLE);
return collection != null ? collection.getElements() : Collections.<JpsArtifact>emptyList();
}
@@ -22,7 +22,7 @@ public class JpsElementCollectionImpl<E extends JpsElement> extends JpsElementBa
myCopyToOriginal = null;
}
public JpsElementCollectionImpl(JpsElementCollectionImpl<E> original) {
private JpsElementCollectionImpl(JpsElementCollectionImpl<E> original) {
myChildRole = original.myChildRole;
myElements = new SmartList<E>();
myCopyToOriginal = new HashMap<E, E>();
@@ -40,8 +40,9 @@ public class JpsElementCollectionImpl<E extends JpsElement> extends JpsElementBa
return myElements;
}
public <X extends E, P extends JpsElement> Iterable<X> getElementsOfType(@NotNull final JpsElementType<P> type) {
return new JpsElementIterable<X>(type);
@Override
public <X extends JpsTypedElement<P>, P extends JpsElement> Iterable<X> getElementsOfType(@NotNull final JpsElementType<P> type) {
return new JpsElementIterable<X, P>(type);
}
@NotNull
@@ -73,6 +74,7 @@ public class JpsElementCollectionImpl<E extends JpsElement> extends JpsElementBa
}
}
@Override
public void removeAllChildren() {
List<E> elements = new ArrayList<E>(myElements);
for (E element : elements) {
@@ -111,19 +113,21 @@ public class JpsElementCollectionImpl<E extends JpsElement> extends JpsElementBa
}
}
private class JpsElementIterable<X extends E> implements Iterable<X> {
private class JpsElementIterable<X extends JpsTypedElement<P>, P extends JpsElement> implements Iterable<X> {
private final JpsElementType<? extends JpsElement> myType;
public JpsElementIterable(JpsElementType<? extends JpsElement> type) {
public JpsElementIterable(JpsElementType<P> type) {
myType = type;
}
@Override
public Iterator<X> iterator() {
return new FilteringIterator<E, X>(myElements.iterator(), new Condition<E>() {
//noinspection unchecked
Iterator<JpsTypedElement<?>> iterator = (Iterator<JpsTypedElement<?>>)myElements.iterator();
return new FilteringIterator<JpsTypedElement<?>, X>(iterator, new Condition<JpsTypedElement<?>>() {
@Override
public boolean value(E e) {
return e instanceof JpsTypedElement && ((JpsTypedElement<?>)e).getType().equals(myType);
public boolean value(JpsTypedElement<?> e) {
return e.getType().equals(myType);
}
});
}
@@ -6,8 +6,8 @@ import org.jetbrains.jps.model.*;
/**
* @author nik
*/
public class JpsElementCollectionRole<E extends JpsElement> extends JpsElementChildRoleBase<JpsElementCollectionImpl<E>>
implements JpsElementCreator<JpsElementCollectionImpl<E>> {
public class JpsElementCollectionRole<E extends JpsElement> extends JpsElementChildRoleBase<JpsElementCollection<E>>
implements JpsElementCreator<JpsElementCollection<E>> {
private final JpsElementChildRole<E> myChildRole;
private JpsElementCollectionRole(@NotNull JpsElementChildRole<E> role) {
@@ -17,7 +17,7 @@ public class JpsElementCollectionRole<E extends JpsElement> extends JpsElementCh
@NotNull
@Override
public JpsElementCollectionImpl<E> create() {
public JpsElementCollection<E> create() {
return new JpsElementCollectionImpl<E>(myChildRole);
}
@@ -29,7 +29,7 @@ public abstract class JpsNamedElementReferenceBase<S extends JpsNamedElement, T
final JpsCompositeElement parent = getParentReference().resolve();
if (parent == null) return null;
JpsElementCollectionImpl<? extends S> collection = getCollection(parent);
JpsElementCollection<? extends S> collection = getCollection(parent);
if (collection == null) return null;
final List<? extends S> elements = collection.getElements();
@@ -45,7 +45,7 @@ public abstract class JpsNamedElementReferenceBase<S extends JpsNamedElement, T
}
@Nullable
protected abstract JpsElementCollectionImpl<? extends S> getCollection(@NotNull JpsCompositeElement parent);
protected abstract JpsElementCollection<? extends S> getCollection(@NotNull JpsCompositeElement parent);
@Nullable
protected abstract T resolve(S element);
@@ -3,6 +3,7 @@ package org.jetbrains.jps.model.impl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.JpsCompositeElement;
import org.jetbrains.jps.model.JpsElementCollection;
import org.jetbrains.jps.model.JpsElementReference;
import org.jetbrains.jps.model.JpsNamedElement;
@@ -29,7 +30,7 @@ public abstract class JpsNamedElementReferenceImpl<T extends JpsNamedElement, Se
}
@Nullable
protected JpsElementCollectionImpl<? extends T> getCollection(@NotNull JpsCompositeElement parent) {
protected JpsElementCollection<? extends T> getCollection(@NotNull JpsCompositeElement parent) {
return parent.getContainer().getChild(myCollectionRole);
}
}
@@ -47,7 +47,7 @@ public class JpsProjectImpl extends JpsRootElementBase<JpsProjectImpl> implement
public
<P extends JpsElement, ModuleType extends JpsModuleType<P> & JpsElementTypeWithDefaultProperties<P>>
JpsModule addModule(@NotNull final String name, @NotNull ModuleType moduleType) {
final JpsElementCollectionImpl<JpsModule> collection = myContainer.getChild(JpsModuleRole.MODULE_COLLECTION_ROLE);
final JpsElementCollection<JpsModule> collection = myContainer.getChild(JpsModuleRole.MODULE_COLLECTION_ROLE);
return collection.addChild(new JpsModuleImpl<P>(moduleType, name, moduleType.createDefaultProperties()));
}
@@ -2,8 +2,8 @@ package org.jetbrains.jps.model.library.impl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jps.model.JpsElement;
import org.jetbrains.jps.model.JpsElementCollection;
import org.jetbrains.jps.model.JpsElementTypeWithDefaultProperties;
import org.jetbrains.jps.model.impl.JpsElementCollectionImpl;
import org.jetbrains.jps.model.library.JpsLibrary;
import org.jetbrains.jps.model.library.JpsLibraryCollection;
import org.jetbrains.jps.model.library.JpsLibraryType;
@@ -15,9 +15,9 @@ import java.util.List;
* @author nik
*/
public class JpsLibraryCollectionImpl implements JpsLibraryCollection {
private final JpsElementCollectionImpl<JpsLibrary> myCollection;
private final JpsElementCollection<JpsLibrary> myCollection;
public JpsLibraryCollectionImpl(JpsElementCollectionImpl<JpsLibrary> collection) {
public JpsLibraryCollectionImpl(JpsElementCollection<JpsLibrary> collection) {
myCollection = collection;
}
@@ -4,8 +4,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.JpsCompositeElement;
import org.jetbrains.jps.model.JpsElement;
import org.jetbrains.jps.model.JpsElementCollection;
import org.jetbrains.jps.model.JpsElementReference;
import org.jetbrains.jps.model.impl.JpsElementCollectionImpl;
import org.jetbrains.jps.model.impl.JpsNamedElementReferenceBase;
import org.jetbrains.jps.model.library.JpsLibrary;
import org.jetbrains.jps.model.library.JpsTypedLibrary;
@@ -47,7 +47,7 @@ public class JpsSdkReferenceImpl<P extends JpsElement> extends JpsNamedElementRe
}
@Nullable
protected JpsElementCollectionImpl<? extends JpsLibrary> getCollection(@NotNull JpsCompositeElement parent) {
protected JpsElementCollection<? extends JpsLibrary> getCollection(@NotNull JpsCompositeElement parent) {
return parent.getContainer().getChild(JpsLibraryRole.LIBRARIES_COLLECTION_ROLE);
}
}
@@ -113,7 +113,7 @@ public class JpsModuleImpl<P extends JpsElement> extends JpsNamedCompositeElemen
@Override
public void removeSourceRoot(@NotNull String url, @NotNull JpsModuleSourceRootType rootType) {
final JpsElementCollectionImpl<JpsModuleSourceRoot> roots = myContainer.getChild(JpsModuleSourceRootRole.ROOT_COLLECTION_ROLE);
final JpsElementCollection<JpsModuleSourceRoot> roots = myContainer.getChild(JpsModuleSourceRootRole.ROOT_COLLECTION_ROLE);
for (JpsModuleSourceRoot root : roots.getElements()) {
if (root.getRootType().equals(rootType) && root.getUrl().equals(url)) {
roots.removeChild(root);
@@ -1,12 +1,22 @@
package org.jetbrains.jps.model;
import com.intellij.util.containers.ContainerUtilRt;
import org.jetbrains.jps.model.java.JpsJavaLibraryType;
import org.jetbrains.jps.model.java.JpsJavaSdkType;
import org.jetbrains.jps.model.library.*;
/**
* @author nik
*/
public class JpsLibraryTest extends JpsModelTestCase {
public void testAddLibrary() {
JpsLibrary a = myProject.addLibrary("a", JpsJavaLibraryType.INSTANCE);
JpsLibraryCollection collection = myProject.getLibraryCollection();
assertSameElements(collection.getLibraries(), a);
assertSameElements(ContainerUtilRt.newArrayList(collection.getLibraries(JpsJavaLibraryType.INSTANCE)), a);
assertEmpty(ContainerUtilRt.newArrayList(collection.getLibraries(JpsJavaSdkType.INSTANCE)));
}
public void testAddRoot() {
final JpsLibrary library = myProject.addLibrary("a", JpsJavaLibraryType.INSTANCE);
library.addRoot("file://my-url", JpsOrderRootType.COMPILED);