Author: cziegeler Date: Thu Jun 8 13:40:42 2017 New Revision: 1798056 URL: http://svn.apache.org/viewvc?rev=1798056&view=rev Log: Add Configurations object and provide iterator over all bundles
Added: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Configurations.java (with props) sling/whiteboard/cziegeler/feature/src/test/java/org/apache/sling/feature/BundlesTest.java (with props) Modified: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Application.java sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Bundles.java sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Feature.java Modified: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Application.java URL: http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Application.java?rev=1798056&r1=1798055&r2=1798056&view=diff ============================================================================== --- sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Application.java (original) +++ sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Application.java Thu Jun 8 13:40:42 2017 @@ -36,7 +36,7 @@ public class Application { private final Bundles bundles = new Bundles(); /** List of configurations. */ - private final List<Configuration> configurations = new ArrayList<>(); + private final Configurations configurations = new Configurations(); /** Map of framework properties. */ private final KeyValueMap frameworkProperties = new KeyValueMap(); @@ -63,7 +63,7 @@ public class Application { * The list is modifiable. * @return The list of configurations */ - public List<Configuration> getConfigurations() { + public Configurations getConfigurations() { return this.configurations; } Modified: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Bundles.java URL: http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Bundles.java?rev=1798056&r1=1798055&r2=1798056&view=diff ============================================================================== --- sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Bundles.java (original) +++ sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Bundles.java Thu Jun 8 13:40:42 2017 @@ -18,14 +18,17 @@ package org.apache.sling.feature; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; +import java.util.NoSuchElementException; import java.util.TreeMap; /** * Bundles groups bundle {@code Artifact}s by start level. */ -public class Bundles { +public class Bundles implements Iterable<Map.Entry<Integer, Artifact>> { /** Map of bundles grouped by start level */ private final Map<Integer, List<Artifact>> startLevelMap = new TreeMap<>(); @@ -108,27 +111,25 @@ public class Bundles { * @return A map entry with start level and artifact, {@code null} otherwise */ public Map.Entry<Integer, Artifact> getSame(final ArtifactId id) { - for(final Map.Entry<Integer, List<Artifact>> entry : this.startLevelMap.entrySet()) { - for(final Artifact artifact : entry.getValue()) { - if ( artifact.getId().isSame(id)) { - return new Map.Entry<Integer, Artifact>() { - - @Override - public Integer getKey() { - return entry.getKey(); - } + for(final Map.Entry<Integer, Artifact> entry : this) { + if ( entry.getValue().getId().isSame(id)) { + return new Map.Entry<Integer, Artifact>() { + + @Override + public Integer getKey() { + return entry.getKey(); + } - @Override - public Artifact getValue() { - return artifact; - } + @Override + public Artifact getValue() { + return entry.getValue(); + } - @Override - public Artifact setValue(final Artifact value) { - throw new IllegalStateException(); - } - }; - } + @Override + public Artifact setValue(final Artifact value) { + throw new IllegalStateException(); + } + }; } } return null; @@ -140,11 +141,9 @@ public class Bundles { * @return {@code true} if the artifact exists */ public boolean containsExact(final ArtifactId id) { - for(final Map.Entry<Integer, List<Artifact>> entry : this.startLevelMap.entrySet()) { - for(final Artifact artifact : entry.getValue()) { - if ( artifact.getId().equals(id)) { - return true; - } + for(final Map.Entry<Integer, Artifact> entry : this) { + if ( entry.getValue().getId().equals(id)) { + return true; } } return false; @@ -156,16 +155,83 @@ public class Bundles { * @return {@code true} if the artifact exists */ public boolean containsSame(final ArtifactId id) { - for(final Map.Entry<Integer, List<Artifact>> entry : this.startLevelMap.entrySet()) { - for(final Artifact artifact : entry.getValue()) { - if ( artifact.getId().isSame(id)) { - return true; - } + for(final Map.Entry<Integer, Artifact> entry : this) { + if ( entry.getValue().getId().isSame(id)) { + return true; } } return false; } + /** + * Iterate over all bundles + */ + @Override + public Iterator<Map.Entry<Integer, Artifact>> iterator() { + final Iterator<Map.Entry<Integer, List<Artifact>>> mainIter = this.startLevelMap.entrySet().iterator(); + return new Iterator<Map.Entry<Integer,Artifact>>() { + + private Map.Entry<Integer, Artifact> next = seek(); + + private Integer level; + + private Iterator<Artifact> innerIter; + + private Map.Entry<Integer, Artifact> seek() { + Map.Entry<Integer, Artifact> entry = null; + while ( this.innerIter != null || mainIter.hasNext() ) { + if ( innerIter != null ) { + if ( innerIter.hasNext() ) { + final Artifact a = innerIter.next(); + final Integer l = this.level; + entry = new Map.Entry<Integer, Artifact>() { + + @Override + public Integer getKey() { + return l; + } + + @Override + public Artifact getValue() { + return a; + } + + @Override + public Artifact setValue(Artifact value) { + throw new UnsupportedOperationException(); + } + }; + break; + } else { + innerIter = null; + } + } else { + final Map.Entry<Integer, List<Artifact>> e = mainIter.next(); + this.level = e.getKey(); + this.innerIter = e.getValue().iterator(); + } + } + return entry; + } + + @Override + public boolean hasNext() { + return this.next != null; + } + + @Override + public Entry<Integer, Artifact> next() { + final Entry<Integer, Artifact> result = next; + if ( result == null ) { + throw new NoSuchElementException(); + } + this.next = seek(); + return result; + } + + }; + } + @Override public String toString() { return "Bundles [" + this.startLevelMap Added: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Configurations.java URL: http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Configurations.java?rev=1798056&view=auto ============================================================================== --- sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Configurations.java (added) +++ sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Configurations.java Thu Jun 8 13:40:42 2017 @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.sling.feature; + +import java.util.ArrayList; + +/** + * A container for configurations. + */ +public class Configurations extends ArrayList<Configuration> { + + private static final long serialVersionUID = -7243822886707856704L; + + /** + * Get the configuration + * @param pid The pid of the configuration + * @return The configuration or {@code null} + */ + public Configuration getConfiguration(final String pid) { + for(final Configuration cfg : this) { + if ( !cfg.isFactoryConfiguration() && pid.equals(cfg.getPid())) { + return cfg; + } + } + return null; + } + + /** + * Get the factory configuration + * @param factoryPid The factoryPid of the configuration + * @param name The name of the configuration + * @return The factory configuration or {@code null} + */ + public Configuration getFactoryConfiguration(final String factoryPid, final String name) { + for(final Configuration cfg : this) { + if ( cfg.isFactoryConfiguration() + && factoryPid.equals(cfg.getFactoryPid()) + && name.equals(cfg.getName())) { + return cfg; + } + } + return null; + } +} Propchange: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Configurations.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Configurations.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Modified: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Feature.java URL: http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Feature.java?rev=1798056&r1=1798055&r2=1798056&view=diff ============================================================================== --- sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Feature.java (original) +++ sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/Feature.java Thu Jun 8 13:40:42 2017 @@ -37,7 +37,7 @@ public class Feature implements Comparab private final Bundles bundles = new Bundles(); - private final List<Configuration> configurations = new ArrayList<>(); + private final Configurations configurations = new Configurations(); private final KeyValueMap frameworkProperties = new KeyValueMap(); @@ -99,7 +99,7 @@ public class Feature implements Comparab return this.bundles; } - public List<Configuration> getConfigurations() { + public Configurations getConfigurations() { return this.configurations; } Added: sling/whiteboard/cziegeler/feature/src/test/java/org/apache/sling/feature/BundlesTest.java URL: http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/src/test/java/org/apache/sling/feature/BundlesTest.java?rev=1798056&view=auto ============================================================================== --- sling/whiteboard/cziegeler/feature/src/test/java/org/apache/sling/feature/BundlesTest.java (added) +++ sling/whiteboard/cziegeler/feature/src/test/java/org/apache/sling/feature/BundlesTest.java Thu Jun 8 13:40:42 2017 @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.feature; + +import static org.junit.Assert.assertEquals; + +import java.util.Map; + +import org.junit.Test; + +public class BundlesTest { + + @Test + public void testIterator() { + final Bundles bundles = new Bundles(); + bundles.add(1, new Artifact(ArtifactId.fromMvnId("1/a/1"))); + bundles.add(5, new Artifact(ArtifactId.fromMvnId("5/a/5"))); + bundles.add(5, new Artifact(ArtifactId.fromMvnId("5/b/6"))); + bundles.add(2, new Artifact(ArtifactId.fromMvnId("2/b/2"))); + bundles.add(2, new Artifact(ArtifactId.fromMvnId("2/a/3"))); + bundles.add(4, new Artifact(ArtifactId.fromMvnId("4/x/4"))); + + int index = 1; + for(final Map.Entry<Integer, Artifact> entry : bundles) { + assertEquals(entry.getKey().toString(), entry.getValue().getId().getGroupId()); + assertEquals(index, entry.getValue().getId().getOSGiVersion().getMajor()); + index++; + } + assertEquals(7, index); + } +} Propchange: sling/whiteboard/cziegeler/feature/src/test/java/org/apache/sling/feature/BundlesTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/whiteboard/cziegeler/feature/src/test/java/org/apache/sling/feature/BundlesTest.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url