001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.sdlctools.models.metabossmodel;
016:
017: import java.util.ArrayList;
018: import java.util.Collection;
019: import java.util.Collections;
020: import java.util.Comparator;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.SortedSet;
024: import java.util.TreeSet;
025:
026: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.Property;
027:
028: /** Set of MetaBossModelElement utility and helper methods realising some facilities seemingly not provided by MDR. */
029: public class ModelElementUtils {
030: // Internally used comparator. Generally speaking compares model elements by name.
031: // In some instances, like Property element - compares by key and not the name
032: private static Comparator sAlphabeticalOrderComparator = new Comparator() {
033: public int compare(Object o1, Object o2) {
034: // Class cast problem will simply result in exception, which is desired outcome
035: ModelElement lModelElement1 = (ModelElement) o1;
036: ModelElement lModelElement2 = (ModelElement) o2;
037: // Check if we are dealing with Properties
038: if ((lModelElement1 instanceof Property)
039: && (lModelElement2 instanceof Property)) {
040: int lResult;
041: String[] lKeyParts1 = ((Property) lModelElement1)
042: .getKey().split("[\\[\\]]");
043: String[] lKeyParts2 = ((Property) lModelElement2)
044: .getKey().split("[\\[\\]]");
045: int lIterateMax = lKeyParts1.length < lKeyParts2.length ? lKeyParts1.length
046: : lKeyParts2.length;
047: for (int i = 0; i < lIterateMax; i++) {
048: if (i % 2 == 0) {
049: // Parts 0,2,4,6 etc are strings and should be compared as strings
050: if ((lResult = lKeyParts1[i]
051: .compareTo(lKeyParts2[i])) != 0)
052: return lResult;
053: } else {
054: // Parts 1,3,5,7 etc are numbers and should be compared as numbers
055: if ((lResult = Integer.valueOf(lKeyParts1[i])
056: .compareTo(
057: Integer.valueOf(lKeyParts2[i]))) != 0)
058: return lResult;
059: }
060: }
061: // Since we are here, the longer key is greater
062: return lKeyParts1.length < lKeyParts2.length ? -1 : 1;
063: }
064: // Null pointer problem will simply result in exception, which is desired outcome
065: return lModelElement1.getName().compareTo(
066: lModelElement2.getName());
067: }
068: };
069:
070: /** This class is a "toolbox" with static utilities and can not be instantiated */
071: private ModelElementUtils() {
072: }
073:
074: /** Returns MetaBossModelElement with required name from the supplied collection or null
075: * if element with this name does not exist */
076: public static ModelElement getModelElement(Collection pCollection,
077: String pElementName) {
078: Iterator lIter = pCollection.iterator();
079: while (lIter.hasNext()) {
080: ModelElement lElement = (ModelElement) lIter.next();
081: if (pElementName.equals(lElement.getName()))
082: return lElement;
083: }
084: return null; // No hits
085: }
086:
087: /** Returns unmodifiable sorted set consisting of all elements of the
088: * source collection sorted by the value of the name attribute in alphabetical order.
089: * Each element of the source collection must be a an instance of the ModelElement otherwise
090: * ClassCastException will occur.
091: * The reason that this class offers two similar sorting methods (getList... and getSet...)
092: * is that List and Set offer user diferent facilities and we are not sure which one will
093: * be more useful.
094: */
095: public static SortedSet getSetOfModelElementsInAlphabeticalOrder(
096: Collection pSourceCollection) {
097: TreeSet lSetToReturn = new TreeSet(sAlphabeticalOrderComparator);
098: lSetToReturn.addAll(pSourceCollection);
099: return Collections.unmodifiableSortedSet(lSetToReturn);
100: }
101:
102: /** Returns unmodifiable list consisting of all elements of the
103: * source collection sorted by the value of the name attribute in alphabetical order.
104: * Each element of the source collection must be a an instance of the ModelElement otherwise
105: * ClassCastException will occur.
106: * The reason that this class offers two similar sorting methods (getList... and getSet...)
107: * is that List and Set offer user diferent facilities and we are not sure which one will
108: * be more useful.
109: */
110: public static List getListOfModelElementsInAlphabeticalOrder(
111: Collection pSourceCollection) {
112: List lListToReturn = new ArrayList(pSourceCollection);
113: Collections.sort(lListToReturn, sAlphabeticalOrderComparator);
114: return Collections.unmodifiableList(lListToReturn);
115: }
116: }
|