diff options
Diffstat (limited to 'plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type')
10 files changed, 1908 insertions, 1908 deletions
diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/CaseInsensitiveString.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/CaseInsensitiveString.java index 9ed7addd944..84a3ba3b07f 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/CaseInsensitiveString.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/CaseInsensitiveString.java @@ -1,64 +1,64 @@ -/*******************************************************************************
- * Copyright (c) 2010 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.framework.jdk.core.type;
-
-/**
- * @author Ryan D. Brooks
- */
-public class CaseInsensitiveString implements CharSequence {
- private final String originalString;
- private final String upperCaseString;
-
- public CaseInsensitiveString(String string) {
- this.originalString = string;
- upperCaseString = string.toUpperCase();
- }
-
- @Override
- public String toString() {
- return originalString;
- }
-
- @Override
- public int hashCode() {
- return ((upperCaseString == null) ? 0 : upperCaseString.hashCode());
- }
-
- @Override
- public boolean equals(Object obj) {
- if (obj instanceof CaseInsensitiveString) {
- CaseInsensitiveString other = ((CaseInsensitiveString) obj);
- if (upperCaseString == null) {
- return other.upperCaseString == null;
- } else {
- return upperCaseString.equals(other.upperCaseString);
- }
- } else if (obj instanceof String) {
- return upperCaseString.equalsIgnoreCase(((String) obj));
- }
- return false;
- }
-
- @Override
- public int length() {
- return originalString == null ? 0 : originalString.length();
- }
-
- @Override
- public char charAt(int index) {
- return originalString.charAt(index);
- }
-
- @Override
- public CharSequence subSequence(int beginIndex, int endIndex) {
- return originalString.subSequence(beginIndex, endIndex);
- }
+/******************************************************************************* + * Copyright (c) 2010 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.type; + +/** + * @author Ryan D. Brooks + */ +public class CaseInsensitiveString implements CharSequence { + private final String originalString; + private final String upperCaseString; + + public CaseInsensitiveString(String string) { + this.originalString = string; + upperCaseString = string.toUpperCase(); + } + + @Override + public String toString() { + return originalString; + } + + @Override + public int hashCode() { + return ((upperCaseString == null) ? 0 : upperCaseString.hashCode()); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof CaseInsensitiveString) { + CaseInsensitiveString other = ((CaseInsensitiveString) obj); + if (upperCaseString == null) { + return other.upperCaseString == null; + } else { + return upperCaseString.equals(other.upperCaseString); + } + } else if (obj instanceof String) { + return upperCaseString.equalsIgnoreCase(((String) obj)); + } + return false; + } + + @Override + public int length() { + return originalString == null ? 0 : originalString.length(); + } + + @Override + public char charAt(int index) { + return originalString.charAt(index); + } + + @Override + public CharSequence subSequence(int beginIndex, int endIndex) { + return originalString.subSequence(beginIndex, endIndex); + } }
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/CompositeKeyHashMap.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/CompositeKeyHashMap.java index 3dcf38e3332..588ab4cda4d 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/CompositeKeyHashMap.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/CompositeKeyHashMap.java @@ -1,199 +1,199 @@ -/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.framework.jdk.core.type;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-
-/**
- * A hash map implementation that uses two objects to form a single composite key. The thread safety of this class
- * is determined by the isThreadSafe of its constructors.
- *
- * @author Ken J. Aguilar
- * @param <KeyOne>
- * @param <KeyTwo>
- * @param <Value>
- */
-public class CompositeKeyHashMap<KeyOne, KeyTwo, Value> implements Map<Pair<KeyOne, KeyTwo>, Value> {
- private final HashCollection<KeyOne, KeyTwo> singleKeyMap;
- private final Map<Pair<KeyOne, KeyTwo>, Value> map;
-
- private final ThreadLocal<Pair<KeyOne, KeyTwo>> threadLocalKey = new ThreadLocal<Pair<KeyOne, KeyTwo>>() {
-
- @Override
- protected Pair<KeyOne, KeyTwo> initialValue() {
- return new Pair<KeyOne, KeyTwo>(null, null);
- }
-
- };
-
- public CompositeKeyHashMap() {
- this(50, false);
- }
-
- public CompositeKeyHashMap(int initialCapacity, boolean isThreadSafe) {
- if (isThreadSafe) {
- map = new ConcurrentHashMap<Pair<KeyOne, KeyTwo>, Value>(initialCapacity);
- } else {
- map = new HashMap<Pair<KeyOne, KeyTwo>, Value>(initialCapacity);
- }
- singleKeyMap = new HashCollection<KeyOne, KeyTwo>(isThreadSafe, HashSet.class);
- }
-
- public void clear() {
- map.clear();
- singleKeyMap.clear();
- }
-
- /**
- * Use this method to determine if any puts(keyOne, anything) have occurred
- *
- * @param keyOne
- * @return whether the map contains the key keyOne
- */
- @SuppressWarnings("unchecked")
- public boolean containsKey(Object key1) {
- return singleKeyMap.containsKey((KeyOne) key1);
- }
-
- /**
- * @param key1
- * @param key2
- * @return whether the map contains the compound key <keyOne, keyTwo>
- */
- public boolean containsKey(KeyOne key1, KeyTwo key2) {
- return map.containsKey(threadLocalKey.get().set(key1, key2));
- }
-
- /**
- * determines if at least one of the compound keys are mapped to this value
- *
- * @param value
- * @return whether the map contains this value
- */
- public boolean containsValue(Object value) {
- return singleKeyMap.containsValue(value);
- }
-
- public Set<Map.Entry<Pair<KeyOne, KeyTwo>, Value>> entrySet() {
- return map.entrySet();
- }
-
- public Value get(Object key) {
- throw new UnsupportedOperationException("use getValues() instead");
- }
-
- public Map<KeyTwo, Value> getKeyedValues(KeyOne key1) {
- Collection<KeyTwo> key2s = singleKeyMap.getValues(key1);
- if (key2s == null) {
- return Collections.emptyMap();
- }
- Map<KeyTwo, Value> values = new HashMap<KeyTwo, Value>(key2s.size());
- for (KeyTwo key2 : key2s) {
- values.put(key2, get(key1, key2));
- }
- return values;
- }
-
- public List<Value> getValues(KeyOne key1) {
- Collection<KeyTwo> key2s = singleKeyMap.getValues(key1);
- if (key2s == null) {
- return Collections.emptyList();
- }
- ArrayList<Value> values = new ArrayList<Value>(key2s.size());
- for (KeyTwo key2 : key2s) {
- values.add(get(key1, key2));
- }
- return values;
- }
-
- public Value get(KeyOne key1, KeyTwo key2) {
- return map.get(threadLocalKey.get().set(key1, key2));
- }
-
- public List<Pair<KeyOne, KeyTwo>> getEnumeratedKeys() {
- List<Pair<KeyOne, KeyTwo>> toReturn = new ArrayList<Pair<KeyOne, KeyTwo>>();
- for (KeyOne firstKey : singleKeyMap.keySet()) {
- for (KeyTwo secondKey : singleKeyMap.getValues(firstKey)) {
- toReturn.add(new Pair<KeyOne, KeyTwo>(firstKey, secondKey));
- }
- }
- return toReturn;
- }
-
- public boolean isEmpty() {
- return map.isEmpty();
- }
-
- public Set<Pair<KeyOne, KeyTwo>> keySet() {
- return map.keySet();
- }
-
- public Value put(Pair<KeyOne, KeyTwo> key, Value value) {
- singleKeyMap.put(key.getFirst(), key.getSecond());
- return map.put(key, value);
- }
-
- public Value put(KeyOne key1, KeyTwo key2, Value value) {
- singleKeyMap.put(key1, key2);
- return map.put(new Pair<KeyOne, KeyTwo>(key1, key2), value);
- }
-
- public void putAll(Map<? extends Pair<KeyOne, KeyTwo>, ? extends Value> copyMap) {
- map.putAll(copyMap);
-
- for (Pair<KeyOne, KeyTwo> key : copyMap.keySet()) {
- singleKeyMap.put(key.getFirst(), key.getSecond());
- }
- }
-
- public Value remove(Object key) {
- throw new UnsupportedOperationException("use removeValues() instead");
- }
-
- /**
- * @param key1
- * @return the previous value associated with key, or null if there was no mapping for key.
- */
- public Collection<Value> removeValues(KeyOne key1) {
- Collection<KeyTwo> key2s = singleKeyMap.getValues(key1);
- if (key2s == null) {
- return null;
- }
- ArrayList<Value> values = new ArrayList<Value>(key2s.size());
- for (KeyTwo key2 : key2s) {
- values.add(map.remove(threadLocalKey.get().set(key1, key2)));
- }
- singleKeyMap.removeValues(key1);
- return values;
- }
-
- public Value remove(KeyOne key1, KeyTwo key2) {
- Value value = map.remove(threadLocalKey.get().set(key1, key2));
- singleKeyMap.removeValue(key1, key2);
- return value;
- }
-
- public int size() {
- return map.size();
- }
-
- public Collection<Value> values() {
- return map.values();
- }
+/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.type; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** + * A hash map implementation that uses two objects to form a single composite key. The thread safety of this class + * is determined by the isThreadSafe of its constructors. + * + * @author Ken J. Aguilar + * @param <KeyOne> + * @param <KeyTwo> + * @param <Value> + */ +public class CompositeKeyHashMap<KeyOne, KeyTwo, Value> implements Map<Pair<KeyOne, KeyTwo>, Value> { + private final HashCollection<KeyOne, KeyTwo> singleKeyMap; + private final Map<Pair<KeyOne, KeyTwo>, Value> map; + + private final ThreadLocal<Pair<KeyOne, KeyTwo>> threadLocalKey = new ThreadLocal<Pair<KeyOne, KeyTwo>>() { + + @Override + protected Pair<KeyOne, KeyTwo> initialValue() { + return new Pair<KeyOne, KeyTwo>(null, null); + } + + }; + + public CompositeKeyHashMap() { + this(50, false); + } + + public CompositeKeyHashMap(int initialCapacity, boolean isThreadSafe) { + if (isThreadSafe) { + map = new ConcurrentHashMap<Pair<KeyOne, KeyTwo>, Value>(initialCapacity); + } else { + map = new HashMap<Pair<KeyOne, KeyTwo>, Value>(initialCapacity); + } + singleKeyMap = new HashCollection<KeyOne, KeyTwo>(isThreadSafe, HashSet.class); + } + + public void clear() { + map.clear(); + singleKeyMap.clear(); + } + + /** + * Use this method to determine if any puts(keyOne, anything) have occurred + * + * @param keyOne + * @return whether the map contains the key keyOne + */ + @SuppressWarnings("unchecked") + public boolean containsKey(Object key1) { + return singleKeyMap.containsKey((KeyOne) key1); + } + + /** + * @param key1 + * @param key2 + * @return whether the map contains the compound key <keyOne, keyTwo> + */ + public boolean containsKey(KeyOne key1, KeyTwo key2) { + return map.containsKey(threadLocalKey.get().set(key1, key2)); + } + + /** + * determines if at least one of the compound keys are mapped to this value + * + * @param value + * @return whether the map contains this value + */ + public boolean containsValue(Object value) { + return singleKeyMap.containsValue(value); + } + + public Set<Map.Entry<Pair<KeyOne, KeyTwo>, Value>> entrySet() { + return map.entrySet(); + } + + public Value get(Object key) { + throw new UnsupportedOperationException("use getValues() instead"); + } + + public Map<KeyTwo, Value> getKeyedValues(KeyOne key1) { + Collection<KeyTwo> key2s = singleKeyMap.getValues(key1); + if (key2s == null) { + return Collections.emptyMap(); + } + Map<KeyTwo, Value> values = new HashMap<KeyTwo, Value>(key2s.size()); + for (KeyTwo key2 : key2s) { + values.put(key2, get(key1, key2)); + } + return values; + } + + public List<Value> getValues(KeyOne key1) { + Collection<KeyTwo> key2s = singleKeyMap.getValues(key1); + if (key2s == null) { + return Collections.emptyList(); + } + ArrayList<Value> values = new ArrayList<Value>(key2s.size()); + for (KeyTwo key2 : key2s) { + values.add(get(key1, key2)); + } + return values; + } + + public Value get(KeyOne key1, KeyTwo key2) { + return map.get(threadLocalKey.get().set(key1, key2)); + } + + public List<Pair<KeyOne, KeyTwo>> getEnumeratedKeys() { + List<Pair<KeyOne, KeyTwo>> toReturn = new ArrayList<Pair<KeyOne, KeyTwo>>(); + for (KeyOne firstKey : singleKeyMap.keySet()) { + for (KeyTwo secondKey : singleKeyMap.getValues(firstKey)) { + toReturn.add(new Pair<KeyOne, KeyTwo>(firstKey, secondKey)); + } + } + return toReturn; + } + + public boolean isEmpty() { + return map.isEmpty(); + } + + public Set<Pair<KeyOne, KeyTwo>> keySet() { + return map.keySet(); + } + + public Value put(Pair<KeyOne, KeyTwo> key, Value value) { + singleKeyMap.put(key.getFirst(), key.getSecond()); + return map.put(key, value); + } + + public Value put(KeyOne key1, KeyTwo key2, Value value) { + singleKeyMap.put(key1, key2); + return map.put(new Pair<KeyOne, KeyTwo>(key1, key2), value); + } + + public void putAll(Map<? extends Pair<KeyOne, KeyTwo>, ? extends Value> copyMap) { + map.putAll(copyMap); + + for (Pair<KeyOne, KeyTwo> key : copyMap.keySet()) { + singleKeyMap.put(key.getFirst(), key.getSecond()); + } + } + + public Value remove(Object key) { + throw new UnsupportedOperationException("use removeValues() instead"); + } + + /** + * @param key1 + * @return the previous value associated with key, or null if there was no mapping for key. + */ + public Collection<Value> removeValues(KeyOne key1) { + Collection<KeyTwo> key2s = singleKeyMap.getValues(key1); + if (key2s == null) { + return null; + } + ArrayList<Value> values = new ArrayList<Value>(key2s.size()); + for (KeyTwo key2 : key2s) { + values.add(map.remove(threadLocalKey.get().set(key1, key2))); + } + singleKeyMap.removeValues(key1); + return values; + } + + public Value remove(KeyOne key1, KeyTwo key2) { + Value value = map.remove(threadLocalKey.get().set(key1, key2)); + singleKeyMap.removeValue(key1, key2); + return value; + } + + public int size() { + return map.size(); + } + + public Collection<Value> values() { + return map.values(); + } }
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/DoublePoint.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/DoublePoint.java index 66f0a7bfe56..43905696ae1 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/DoublePoint.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/DoublePoint.java @@ -1,84 +1,84 @@ -/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.framework.jdk.core.type;
-
-import java.io.Serializable;
-
-/**
- * @author Charles Shaw
- */
-public class DoublePoint implements Serializable{
-
- /**
- *
- */
- private static final long serialVersionUID = 2417895993917844086L;
-protected double x;
- protected double y;
-
- /**
- * @param x The x coordinate of the point.
- * @param y The y coordinate of the point.
- */
- public DoublePoint(double x, double y) {
- super();
- this.x = x;
- this.y = y;
- }
-
- public DoublePoint() {
- super();
- this.x = 0.0;
- this.y = 0.0;
- }
-
- public double getX() {
- return x;
- }
-
- public void setX(double x) {
- this.x = x;
- }
-
- public double getY() {
- return y;
- }
-
- public void setY(double y) {
- this.y = y;
- }
-
- @Override
- public boolean equals(Object object) {
- boolean matches = false;
-
- if (object instanceof DoublePoint) {
- DoublePoint point = (DoublePoint) object;
- matches = point.x == this.x && point.y == this.y;
- }
-
- return matches;
- }
-
- @Override
- public int hashCode() {
- int result = 17;
- int prime = 31;
- result = result * prime + ((Double) x).hashCode();
- result = result * prime + ((Double) y).hashCode();
- return result;
- }
-
- @Override
- public String toString() {
- return "(" + x + ", " + y + ")";
- }
-}
+/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.type; + +import java.io.Serializable; + +/** + * @author Charles Shaw + */ +public class DoublePoint implements Serializable{ + + /** + * + */ + private static final long serialVersionUID = 2417895993917844086L; +protected double x; + protected double y; + + /** + * @param x The x coordinate of the point. + * @param y The y coordinate of the point. + */ + public DoublePoint(double x, double y) { + super(); + this.x = x; + this.y = y; + } + + public DoublePoint() { + super(); + this.x = 0.0; + this.y = 0.0; + } + + public double getX() { + return x; + } + + public void setX(double x) { + this.x = x; + } + + public double getY() { + return y; + } + + public void setY(double y) { + this.y = y; + } + + @Override + public boolean equals(Object object) { + boolean matches = false; + + if (object instanceof DoublePoint) { + DoublePoint point = (DoublePoint) object; + matches = point.x == this.x && point.y == this.y; + } + + return matches; + } + + @Override + public int hashCode() { + int result = 17; + int prime = 31; + result = result * prime + ((Double) x).hashCode(); + result = result * prime + ((Double) y).hashCode(); + return result; + } + + @Override + public String toString() { + return "(" + x + ", " + y + ")"; + } +} diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/HashCollectionPlus.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/HashCollectionPlus.java index 59d9cbb482b..b98a8e3856b 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/HashCollectionPlus.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/HashCollectionPlus.java @@ -1,330 +1,330 @@ -/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.framework.jdk.core.type;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-
-/**
- * A Map of keys to multiple values. Collections of values are stored in the Map. The type of Collection can be
- * specified at construction, if desired. All Collections returned by methods are backed by the HashCollection, so
- * changes to the HashCollection are reflected in the Collection, and vice-versa. However, modifications to the
- * Collection outside of this class are generally discouraged because removal of the last item would then not guarantee
- * removal of the key. The mapping also contains a "plus" object. This object can store additional information about the
- * key. At construction a class of type IPlusProvider must be provided. This provider will generate instances of this
- * "plus" object whenever a new key is added to the Map. When all of the items in the Collection are removed, the entire
- * key is removed from the table - therefore, the "plus" object is only available as long as the Collection for a given
- * key is not empty.
- *
- * @author David Diepenbrock
- */
-public class HashCollectionPlus<K, V, O> {
-
- private boolean isSynchronized;
- @SuppressWarnings("unchecked")
- private Class<? extends Collection> collectionType;
- private IPlusProvider<O> plusProvider;
-
- private Map<K, Pair<Collection<V>, O>> map;
- @SuppressWarnings("unchecked")
- public static final Class<? extends Collection> DEFAULT_COLLECTION_TYPE = ArrayList.class;
-
- /********************************************************************************************************************
- * Constructors
- *******************************************************************************************************************/
-
- /**
- * @param isSynchronized - If true, the Map & Collection will both be synchronized using the
- * Collections.synchronizedMap & Collections.synchronizedCollection. otherwise, this class will not be
- * synchronzied and therefore not threadsafe.
- * @param collectionType The type of collection to use to as the values within the HashMap.
- * @param initialCapacity
- * @param plusProvider
- * @see HashMap#HashMap(int, float)
- */
- @SuppressWarnings("unchecked")
- public HashCollectionPlus(boolean isSynchronized, Class<? extends Collection> collectionType, int initialCapacity, float loadFactor, IPlusProvider<O> plusProvider) {
-
- if (isSynchronized) {
- map = new ConcurrentHashMap<K, Pair<Collection<V>, O>>(initialCapacity, loadFactor);
- } else {
- map = new HashMap<K, Pair<Collection<V>, O>>(initialCapacity, loadFactor);
- }
-
- this.isSynchronized = isSynchronized;
- this.collectionType = collectionType;
- this.plusProvider = plusProvider;
- }
-
- /**
- * @param isSynchronized - If true, the Map & Collection will both be synchronized using the
- * Collections.synchronizedMap & Collections.synchronizedCollection. otherwise, this class will not be
- * synchronzied and therefore not threadsafe.
- * @param collectionType - The type of collection to use to as the values within the HashMap.
- * @param initialCapacity
- * @param plusProvider
- * @see HashMap#HashMap(int)
- */
- @SuppressWarnings("unchecked")
- public HashCollectionPlus(boolean isSynchronized, Class<? extends Collection> collectionType, int initialCapacity, IPlusProvider<O> plusProvider) {
- if (isSynchronized) {
- map = new ConcurrentHashMap<K, Pair<Collection<V>, O>>(initialCapacity);
- } else {
- map = new HashMap<K, Pair<Collection<V>, O>>(initialCapacity);
- }
-
- this.isSynchronized = isSynchronized;
- this.collectionType = collectionType;
- this.plusProvider = plusProvider;
- }
-
- /**
- * @param isSynchronized - If true, the Map & Collection will both be synchronized using the
- * Collections.synchronizedMap & Collections.synchronizedCollection. otherwise, this class will not be
- * synchronzied and therefore not threadsafe.
- * @param collectionType - The type of collection to use to as the values within the HashMap.
- * @param plusProvider
- * @see HashMap#HashMap()
- */
- @SuppressWarnings("unchecked")
- public HashCollectionPlus(boolean isSynchronized, Class<? extends Collection> collectionType, IPlusProvider<O> plusProvider) {
- if (isSynchronized) {
- map = new ConcurrentHashMap<K, Pair<Collection<V>, O>>();
- } else {
- map = new HashMap<K, Pair<Collection<V>, O>>();
- }
-
- this.isSynchronized = isSynchronized;
- this.collectionType = collectionType;
- this.plusProvider = plusProvider;
- }
-
- /**
- * Creates an unsynchronized HashCollectionPlus using a default Collection type (ArrayList)
- *
- * @see HashMap#HashMap(int, float)
- */
- public HashCollectionPlus(int initialCapacity, float loadFactor, IPlusProvider<O> plusProvider) {
- this(false, DEFAULT_COLLECTION_TYPE, initialCapacity, loadFactor, plusProvider);
- }
-
- /**
- * Creates an unsynchronized HashCollectionPlus using a default Collection type (ArrayList)
- *
- * @see HashMap#HashMap(int)
- */
- public HashCollectionPlus(int initialCapacity, IPlusProvider<O> plusProvider) {
- this(false, DEFAULT_COLLECTION_TYPE, initialCapacity, plusProvider);
- }
-
- /**
- * Creates an unsynchronized HashCollectionPlus using a default Collection type (ArrayList)
- *
- * @see HashMap#HashMap()
- */
- public HashCollectionPlus(IPlusProvider<O> plusProvider) {
- this(false, DEFAULT_COLLECTION_TYPE, plusProvider);
- }
-
- /********************************************************************************************************************
- * Methods
- *******************************************************************************************************************/
-
- /**
- * Adds the value to the collection specified by the key. If there is not a collection for the given key, a new
- * collection is created and added to the hash.
- *
- * @param key The key whose collection we will add value to.
- * @param value The value to be added.
- * @return the collection containing value and all other items associated with the key.
- */
- @SuppressWarnings("unchecked")
- public Collection<V> put(K key, V value) {
- Pair<Collection<V>, O> objectPair = map.get(key);
- if (objectPair == null) {
- try {
- Collection<V> items;
- if (isSynchronized) {
- items = Collections.synchronizedCollection(collectionType.newInstance());
- } else {
- items = collectionType.newInstance();
- }
-
- objectPair = new Pair(items, plusProvider.newObject());
- map.put(key, objectPair);
- } catch (InstantiationException ex) {
- ex.printStackTrace();
- return null;
- } catch (IllegalAccessException ex) {
- ex.printStackTrace();
- return null;
- }
- }
- objectPair.getFirst().add(value);
- return objectPair.getFirst();
- }
-
- /**
- * Adds all of the items in the Collection values to the collection for the specified key.
- *
- * @param key The key to add the values to
- * @param values The values to be added
- * @return The collection for the key, containing all values.
- */
- public Collection<V> put(K key, Collection<V> values) {
- Collection<V> items = null;
-
- for (V value : values) {
- if (items == null) {
- items = this.put(key, value);
- } else {
- items.add(value);
- }
- }
- return items;
- }
-
- /**
- * @param key The key whose collection we will remove value from.
- * @param value The value to be removed
- * @return true iff the value was removed from the collection for key.
- */
- public boolean removeValue(K key, V value) {
- Pair<Collection<V>, O> objectPair = map.get(key);
-
- if (objectPair != null) {
- Collection<V> items = objectPair.getFirst();
- if (items != null) {
- if (items.remove(value)) {
- if (items.isEmpty()) {
- map.remove(key);
- }
- return true;
- }
- }
- }
- return false;
- }
-
- public Collection<V> removeValues(K key) {
- Pair<Collection<V>, O> objectPair = map.remove(key);
- Collection<V> toReturn;
- if (objectPair != null) {
- toReturn = objectPair.getFirst();
- } else {
- toReturn = Collections.emptyList();
- }
- return toReturn;
- }
-
- /**
- * Returns the Collection of items for this key, or null if the key does not exist.
- *
- * @param key
- * @return Return value collection reference
- */
- public Collection<V> getValues(K key) {
- Pair<Collection<V>, O> objectPair = map.get(key);
- if (objectPair != null) {
- return objectPair.getFirst();
- }
- return null;
- }
-
- /**
- * Returns the Collection all items
- *
- * @param key
- * @return Return value collection reference
- */
- public List<V> getValues() {
- List<V> values = new ArrayList<V>();
- for (Pair<Collection<V>, O> objectPair : map.values()) {
- if (objectPair != null) {
- values.addAll(objectPair.getFirst());
- }
- }
- return values;
- }
-
- /**
- * Returns the "plus" object associated with the key, or null if the key does not exist.
- *
- * @param key
- * @return Return object reference
- */
- public O getPlusObject(K key) {
- Pair<Collection<V>, O> objectPair = map.get(key);
- if (objectPair != null) {
- return map.get(key).getSecond();
- }
- return null;
- }
-
- public Set<K> keySet() {
- return map.keySet();
- }
-
- public void clear() {
- map.clear();
- }
-
- public boolean containsKey(K key) {
- return map.containsKey(key);
- }
-
- public boolean isEmpty() {
- return map.isEmpty();
- }
-
- /**
- * The total number of key-value combinations
- */
- public int size() {
- int size = 0;
- Set<K> keySet = keySet();
-
- synchronized (map) {
- for (K key : keySet) {
- size += getValues(key).size();
- }
- }
- return size;
- }
-
- /**
- * @param value
- * @return whether the map contains this value
- */
- public boolean containsValue(Object value) {
- for (Pair<Collection<V>, O> objectPair : map.values()) {
- if (objectPair != null) {
- for (V tempValue : objectPair.getFirst()) {
- if (value.equals(tempValue)) {
- return true;
- }
- }
- }
- }
- return false;
- }
-
- @Override
- public String toString() {
- return map.toString();
- }
-}
+/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.type; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** + * A Map of keys to multiple values. Collections of values are stored in the Map. The type of Collection can be + * specified at construction, if desired. All Collections returned by methods are backed by the HashCollection, so + * changes to the HashCollection are reflected in the Collection, and vice-versa. However, modifications to the + * Collection outside of this class are generally discouraged because removal of the last item would then not guarantee + * removal of the key. The mapping also contains a "plus" object. This object can store additional information about the + * key. At construction a class of type IPlusProvider must be provided. This provider will generate instances of this + * "plus" object whenever a new key is added to the Map. When all of the items in the Collection are removed, the entire + * key is removed from the table - therefore, the "plus" object is only available as long as the Collection for a given + * key is not empty. + * + * @author David Diepenbrock + */ +public class HashCollectionPlus<K, V, O> { + + private boolean isSynchronized; + @SuppressWarnings("unchecked") + private Class<? extends Collection> collectionType; + private IPlusProvider<O> plusProvider; + + private Map<K, Pair<Collection<V>, O>> map; + @SuppressWarnings("unchecked") + public static final Class<? extends Collection> DEFAULT_COLLECTION_TYPE = ArrayList.class; + + /******************************************************************************************************************** + * Constructors + *******************************************************************************************************************/ + + /** + * @param isSynchronized - If true, the Map & Collection will both be synchronized using the + * Collections.synchronizedMap & Collections.synchronizedCollection. otherwise, this class will not be + * synchronzied and therefore not threadsafe. + * @param collectionType The type of collection to use to as the values within the HashMap. + * @param initialCapacity + * @param plusProvider + * @see HashMap#HashMap(int, float) + */ + @SuppressWarnings("unchecked") + public HashCollectionPlus(boolean isSynchronized, Class<? extends Collection> collectionType, int initialCapacity, float loadFactor, IPlusProvider<O> plusProvider) { + + if (isSynchronized) { + map = new ConcurrentHashMap<K, Pair<Collection<V>, O>>(initialCapacity, loadFactor); + } else { + map = new HashMap<K, Pair<Collection<V>, O>>(initialCapacity, loadFactor); + } + + this.isSynchronized = isSynchronized; + this.collectionType = collectionType; + this.plusProvider = plusProvider; + } + + /** + * @param isSynchronized - If true, the Map & Collection will both be synchronized using the + * Collections.synchronizedMap & Collections.synchronizedCollection. otherwise, this class will not be + * synchronzied and therefore not threadsafe. + * @param collectionType - The type of collection to use to as the values within the HashMap. + * @param initialCapacity + * @param plusProvider + * @see HashMap#HashMap(int) + */ + @SuppressWarnings("unchecked") + public HashCollectionPlus(boolean isSynchronized, Class<? extends Collection> collectionType, int initialCapacity, IPlusProvider<O> plusProvider) { + if (isSynchronized) { + map = new ConcurrentHashMap<K, Pair<Collection<V>, O>>(initialCapacity); + } else { + map = new HashMap<K, Pair<Collection<V>, O>>(initialCapacity); + } + + this.isSynchronized = isSynchronized; + this.collectionType = collectionType; + this.plusProvider = plusProvider; + } + + /** + * @param isSynchronized - If true, the Map & Collection will both be synchronized using the + * Collections.synchronizedMap & Collections.synchronizedCollection. otherwise, this class will not be + * synchronzied and therefore not threadsafe. + * @param collectionType - The type of collection to use to as the values within the HashMap. + * @param plusProvider + * @see HashMap#HashMap() + */ + @SuppressWarnings("unchecked") + public HashCollectionPlus(boolean isSynchronized, Class<? extends Collection> collectionType, IPlusProvider<O> plusProvider) { + if (isSynchronized) { + map = new ConcurrentHashMap<K, Pair<Collection<V>, O>>(); + } else { + map = new HashMap<K, Pair<Collection<V>, O>>(); + } + + this.isSynchronized = isSynchronized; + this.collectionType = collectionType; + this.plusProvider = plusProvider; + } + + /** + * Creates an unsynchronized HashCollectionPlus using a default Collection type (ArrayList) + * + * @see HashMap#HashMap(int, float) + */ + public HashCollectionPlus(int initialCapacity, float loadFactor, IPlusProvider<O> plusProvider) { + this(false, DEFAULT_COLLECTION_TYPE, initialCapacity, loadFactor, plusProvider); + } + + /** + * Creates an unsynchronized HashCollectionPlus using a default Collection type (ArrayList) + * + * @see HashMap#HashMap(int) + */ + public HashCollectionPlus(int initialCapacity, IPlusProvider<O> plusProvider) { + this(false, DEFAULT_COLLECTION_TYPE, initialCapacity, plusProvider); + } + + /** + * Creates an unsynchronized HashCollectionPlus using a default Collection type (ArrayList) + * + * @see HashMap#HashMap() + */ + public HashCollectionPlus(IPlusProvider<O> plusProvider) { + this(false, DEFAULT_COLLECTION_TYPE, plusProvider); + } + + /******************************************************************************************************************** + * Methods + *******************************************************************************************************************/ + + /** + * Adds the value to the collection specified by the key. If there is not a collection for the given key, a new + * collection is created and added to the hash. + * + * @param key The key whose collection we will add value to. + * @param value The value to be added. + * @return the collection containing value and all other items associated with the key. + */ + @SuppressWarnings("unchecked") + public Collection<V> put(K key, V value) { + Pair<Collection<V>, O> objectPair = map.get(key); + if (objectPair == null) { + try { + Collection<V> items; + if (isSynchronized) { + items = Collections.synchronizedCollection(collectionType.newInstance()); + } else { + items = collectionType.newInstance(); + } + + objectPair = new Pair(items, plusProvider.newObject()); + map.put(key, objectPair); + } catch (InstantiationException ex) { + ex.printStackTrace(); + return null; + } catch (IllegalAccessException ex) { + ex.printStackTrace(); + return null; + } + } + objectPair.getFirst().add(value); + return objectPair.getFirst(); + } + + /** + * Adds all of the items in the Collection values to the collection for the specified key. + * + * @param key The key to add the values to + * @param values The values to be added + * @return The collection for the key, containing all values. + */ + public Collection<V> put(K key, Collection<V> values) { + Collection<V> items = null; + + for (V value : values) { + if (items == null) { + items = this.put(key, value); + } else { + items.add(value); + } + } + return items; + } + + /** + * @param key The key whose collection we will remove value from. + * @param value The value to be removed + * @return true iff the value was removed from the collection for key. + */ + public boolean removeValue(K key, V value) { + Pair<Collection<V>, O> objectPair = map.get(key); + + if (objectPair != null) { + Collection<V> items = objectPair.getFirst(); + if (items != null) { + if (items.remove(value)) { + if (items.isEmpty()) { + map.remove(key); + } + return true; + } + } + } + return false; + } + + public Collection<V> removeValues(K key) { + Pair<Collection<V>, O> objectPair = map.remove(key); + Collection<V> toReturn; + if (objectPair != null) { + toReturn = objectPair.getFirst(); + } else { + toReturn = Collections.emptyList(); + } + return toReturn; + } + + /** + * Returns the Collection of items for this key, or null if the key does not exist. + * + * @param key + * @return Return value collection reference + */ + public Collection<V> getValues(K key) { + Pair<Collection<V>, O> objectPair = map.get(key); + if (objectPair != null) { + return objectPair.getFirst(); + } + return null; + } + + /** + * Returns the Collection all items + * + * @param key + * @return Return value collection reference + */ + public List<V> getValues() { + List<V> values = new ArrayList<V>(); + for (Pair<Collection<V>, O> objectPair : map.values()) { + if (objectPair != null) { + values.addAll(objectPair.getFirst()); + } + } + return values; + } + + /** + * Returns the "plus" object associated with the key, or null if the key does not exist. + * + * @param key + * @return Return object reference + */ + public O getPlusObject(K key) { + Pair<Collection<V>, O> objectPair = map.get(key); + if (objectPair != null) { + return map.get(key).getSecond(); + } + return null; + } + + public Set<K> keySet() { + return map.keySet(); + } + + public void clear() { + map.clear(); + } + + public boolean containsKey(K key) { + return map.containsKey(key); + } + + public boolean isEmpty() { + return map.isEmpty(); + } + + /** + * The total number of key-value combinations + */ + public int size() { + int size = 0; + Set<K> keySet = keySet(); + + synchronized (map) { + for (K key : keySet) { + size += getValues(key).size(); + } + } + return size; + } + + /** + * @param value + * @return whether the map contains this value + */ + public boolean containsValue(Object value) { + for (Pair<Collection<V>, O> objectPair : map.values()) { + if (objectPair != null) { + for (V tempValue : objectPair.getFirst()) { + if (value.equals(tempValue)) { + return true; + } + } + } + } + return false; + } + + @Override + public String toString() { + return map.toString(); + } +} diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/IPropertyStore.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/IPropertyStore.java index a861c783564..ef745084e67 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/IPropertyStore.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/IPropertyStore.java @@ -1,199 +1,199 @@ -/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.framework.jdk.core.type;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.Set;
-
-/**
- * @author Roberto E. Escobar
- */
-public interface IPropertyStore {
-
- /**
- * Returns the value of the given key .
- *
- * @param key the key
- * @return the value, or <code>null</code> if none
- */
- public String get(String key);
-
- /**
- * Returns the value, an array of strings, of the given key.
- *
- * @param key the key
- * @return the array of string, or <code>null</code> if none
- */
- public String[] getArray(String key);
-
- /**
- * Convert the value of the given key to a boolean and return it.
- *
- * @param key the key
- * @return the boolean value, or <code>false</code> if none
- */
- public boolean getBoolean(String key);
-
- /**
- * Convert the value of the given key to a double and return it.
- *
- * @param key the key
- * @return the value converted to double, or throws <code>NumberFormatException</code> if none
- * @exception NumberFormatException if the string value does not contain a parsable number.
- * @see java.lang.Double#valueOf(java.lang.String)
- */
- public double getDouble(String key) throws NumberFormatException;
-
- /**
- * Convert the value of the given key to a float and return it.
- *
- * @param key the key
- * @return the value converted to float, or throws <code>NumberFormatException</code> if none
- * @exception NumberFormatException if the string value does not contain a parsable number.
- * @see java.lang.Float#valueOf(java.lang.String)
- */
- public float getFloat(String key) throws NumberFormatException;
-
- /**
- * Convert the value of the given key to a int and return it.
- *
- * @param key the key
- * @return the value converted to int, or throws <code>NumberFormatException</code> if none
- * @exception NumberFormatException if the string value does not contain a parsable number.
- * @see java.lang.Integer#valueOf(java.lang.String)
- */
- public int getInt(String key) throws NumberFormatException;
-
- /**
- * Convert the value of the given key to a long and return it.
- *
- * @param key the key
- * @return the value converted to long, or throws <code>NumberFormatException</code> if none
- * @exception NumberFormatException if the string value does not contain a parsable number.
- * @see java.lang.Long#valueOf(java.lang.String)
- */
- public long getLong(String key) throws NumberFormatException;
-
- /**
- * Convert the value of the given key to a IPropertyStore and return it.
- *
- * @param key the key
- * @return the value converted to a PropertyStore, or null if key was is not found
- */
- public IPropertyStore getPropertyStore(String key);
-
- /**
- * Adds the pair <code>key/value</code>.
- *
- * @param key the key.
- * @param value the value to be associated with the <code>key</code>
- */
- public void put(String key, String[] value);
-
- /**
- * Converts the double <code>value</code> to a string and adds the pair <code>key/value</code>.
- *
- * @param key the key.
- * @param value the value to be associated with the <code>key</code>
- */
- public void put(String key, double value);
-
- /**
- * Converts the float <code>value</code> to a string and adds the pair <code>key/value</code>.
- *
- * @param key the key.
- * @param value the value to be associated with the <code>key</code>
- */
- public void put(String key, float value);
-
- /**
- * Converts the integer <code>value</code> to a string and adds the pair <code>key/value</code>.
- *
- * @param key the key.
- * @param value the value to be associated with the <code>key</code>
- */
- public void put(String key, int value);
-
- /**
- * Converts the long <code>value</code> to a string and adds the pair <code>key/value</code>.
- *
- * @param key the key.
- * @param value the value to be associated with the <code>key</code>
- */
- public void put(String key, long value);
-
- /**
- * Adds the pair <code>key/value</code>.
- *
- * @param key the key.
- * @param value the value to be associated with the <code>key</code>
- */
- public void put(String key, String value);
-
- /**
- * Converts the boolean <code>value</code> to a string and adds the pair <code>key/value</code>.
- *
- * @param key the key.
- * @param value the value to be associated with the <code>key</code>
- */
- public void put(String key, boolean value);
-
- /**
- * Returns the property store's id
- *
- * @return The Property Store's id
- */
- public String getId();
-
- /**
- * Save a property store to an outputStream.
- *
- * @param outputStream the outputStream to write to.
- * @throws IOException
- */
- public void save(OutputStream outputStream) throws Exception;
-
- /**
- * Loads a property store from an inputStream.
- *
- * @param inputStream to read property store values from.
- * @throws IOException
- */
- public void load(InputStream inputStream) throws Exception;
-
- /**
- * Get keys referencing primitive type items
- *
- * @return primitive type item keys
- */
- public Set<String> keySet();
-
- /**
- * @return whether the propertyStore is empty
- */
- public boolean isEmpty();
-
- /**
- * Get keys referencing arrayItems
- *
- * @return array item keys
- */
- public Set<String> arrayKeySet();
-
- /**
- * Get keys referencing inner property store items
- *
- * @return array item keys
- */
- public Set<String> innerStoresKeySet();
-}
+/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.type; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Set; + +/** + * @author Roberto E. Escobar + */ +public interface IPropertyStore { + + /** + * Returns the value of the given key . + * + * @param key the key + * @return the value, or <code>null</code> if none + */ + public String get(String key); + + /** + * Returns the value, an array of strings, of the given key. + * + * @param key the key + * @return the array of string, or <code>null</code> if none + */ + public String[] getArray(String key); + + /** + * Convert the value of the given key to a boolean and return it. + * + * @param key the key + * @return the boolean value, or <code>false</code> if none + */ + public boolean getBoolean(String key); + + /** + * Convert the value of the given key to a double and return it. + * + * @param key the key + * @return the value converted to double, or throws <code>NumberFormatException</code> if none + * @exception NumberFormatException if the string value does not contain a parsable number. + * @see java.lang.Double#valueOf(java.lang.String) + */ + public double getDouble(String key) throws NumberFormatException; + + /** + * Convert the value of the given key to a float and return it. + * + * @param key the key + * @return the value converted to float, or throws <code>NumberFormatException</code> if none + * @exception NumberFormatException if the string value does not contain a parsable number. + * @see java.lang.Float#valueOf(java.lang.String) + */ + public float getFloat(String key) throws NumberFormatException; + + /** + * Convert the value of the given key to a int and return it. + * + * @param key the key + * @return the value converted to int, or throws <code>NumberFormatException</code> if none + * @exception NumberFormatException if the string value does not contain a parsable number. + * @see java.lang.Integer#valueOf(java.lang.String) + */ + public int getInt(String key) throws NumberFormatException; + + /** + * Convert the value of the given key to a long and return it. + * + * @param key the key + * @return the value converted to long, or throws <code>NumberFormatException</code> if none + * @exception NumberFormatException if the string value does not contain a parsable number. + * @see java.lang.Long#valueOf(java.lang.String) + */ + public long getLong(String key) throws NumberFormatException; + + /** + * Convert the value of the given key to a IPropertyStore and return it. + * + * @param key the key + * @return the value converted to a PropertyStore, or null if key was is not found + */ + public IPropertyStore getPropertyStore(String key); + + /** + * Adds the pair <code>key/value</code>. + * + * @param key the key. + * @param value the value to be associated with the <code>key</code> + */ + public void put(String key, String[] value); + + /** + * Converts the double <code>value</code> to a string and adds the pair <code>key/value</code>. + * + * @param key the key. + * @param value the value to be associated with the <code>key</code> + */ + public void put(String key, double value); + + /** + * Converts the float <code>value</code> to a string and adds the pair <code>key/value</code>. + * + * @param key the key. + * @param value the value to be associated with the <code>key</code> + */ + public void put(String key, float value); + + /** + * Converts the integer <code>value</code> to a string and adds the pair <code>key/value</code>. + * + * @param key the key. + * @param value the value to be associated with the <code>key</code> + */ + public void put(String key, int value); + + /** + * Converts the long <code>value</code> to a string and adds the pair <code>key/value</code>. + * + * @param key the key. + * @param value the value to be associated with the <code>key</code> + */ + public void put(String key, long value); + + /** + * Adds the pair <code>key/value</code>. + * + * @param key the key. + * @param value the value to be associated with the <code>key</code> + */ + public void put(String key, String value); + + /** + * Converts the boolean <code>value</code> to a string and adds the pair <code>key/value</code>. + * + * @param key the key. + * @param value the value to be associated with the <code>key</code> + */ + public void put(String key, boolean value); + + /** + * Returns the property store's id + * + * @return The Property Store's id + */ + public String getId(); + + /** + * Save a property store to an outputStream. + * + * @param outputStream the outputStream to write to. + * @throws IOException + */ + public void save(OutputStream outputStream) throws Exception; + + /** + * Loads a property store from an inputStream. + * + * @param inputStream to read property store values from. + * @throws IOException + */ + public void load(InputStream inputStream) throws Exception; + + /** + * Get keys referencing primitive type items + * + * @return primitive type item keys + */ + public Set<String> keySet(); + + /** + * @return whether the propertyStore is empty + */ + public boolean isEmpty(); + + /** + * Get keys referencing arrayItems + * + * @return array item keys + */ + public Set<String> arrayKeySet(); + + /** + * Get keys referencing inner property store items + * + * @return array item keys + */ + public Set<String> innerStoresKeySet(); +} diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/IVariantData.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/IVariantData.java index 84cda2ca355..74a639efb9b 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/IVariantData.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/IVariantData.java @@ -1,61 +1,61 @@ -/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.osee.framework.jdk.core.type;
-
-import java.io.InputStream;
-import java.util.Date;
-
-/**
- * @author Roberto E. Escobar
- */
-public interface IVariantData {
-
- public String get(String key);
-
- public boolean isEmpty(String key);
-
- public String[] getArray(String key);
-
- public boolean getBoolean(String key);
-
- public double getDouble(String key) throws NumberFormatException;
-
- public float getFloat(String key) throws NumberFormatException;
-
- public int getInt(String key) throws NumberFormatException;
-
- public long getLong(String key) throws NumberFormatException;
-
- public Date getDate(String key) throws IllegalArgumentException;
-
- public String getStreamAsString(String key) throws Exception;
-
- public InputStream getStream(String key) throws IllegalArgumentException;
-
- public void put(String key, String[] value);
-
- public void put(String key, double value);
-
- public void put(String key, float value);
-
- public void put(String key, int value);
-
- public void put(String key, long value);
-
- public void put(String key, String value);
-
- public void put(String key, boolean value);
-
- public void put(String key, Date date);
-
- public void put(String key, byte[] bytes);
-}
+/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ + +package org.eclipse.osee.framework.jdk.core.type; + +import java.io.InputStream; +import java.util.Date; + +/** + * @author Roberto E. Escobar + */ +public interface IVariantData { + + public String get(String key); + + public boolean isEmpty(String key); + + public String[] getArray(String key); + + public boolean getBoolean(String key); + + public double getDouble(String key) throws NumberFormatException; + + public float getFloat(String key) throws NumberFormatException; + + public int getInt(String key) throws NumberFormatException; + + public long getLong(String key) throws NumberFormatException; + + public Date getDate(String key) throws IllegalArgumentException; + + public String getStreamAsString(String key) throws Exception; + + public InputStream getStream(String key) throws IllegalArgumentException; + + public void put(String key, String[] value); + + public void put(String key, double value); + + public void put(String key, float value); + + public void put(String key, int value); + + public void put(String key, long value); + + public void put(String key, String value); + + public void put(String key, boolean value); + + public void put(String key, Date date); + + public void put(String key, byte[] bytes); +} diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/Pair.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/Pair.java index a613459a83d..f03ed8fd5e5 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/Pair.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/Pair.java @@ -1,117 +1,117 @@ -/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.framework.jdk.core.type;
-
-import java.io.Serializable;
-
-/**
- * @author Roberto E. Escobar
- */
-public class Pair<T, K> implements Serializable {
- private static final long serialVersionUID = 1764353834209869140L;
- private T first;
- private K second;
-
- public Pair(T first, K second) {
- this.first = first;
- this.second = second;
- }
-
- public T getFirst() {
- return first;
- }
-
- public K getSecond() {
- return second;
- }
-
- public void setFirst(T key) {
- this.first = key;
- }
-
- public void setSecond(K value) {
- this.second = value;
- }
-
- public Pair<T, K> set(T first, K second) {
- this.first = first;
- this.second = second;
- return this;
- }
-
- @Override
- public String toString() {
- String firstString = String.valueOf(first);
- String secondString = String.valueOf(second);
- return String.format("[%s, %s]", firstString, secondString);
- }
-
- @Override
- public boolean equals(Object obj) {
- boolean result = false;
- if (obj instanceof Pair<?, ?>) {
- Pair<?, ?> other = (Pair<?, ?>) obj;
- boolean left = first == null ? other.first == null : first.equals(other.first);
- boolean right = second == null ? other.second == null : second.equals(other.second);
- result = left && right;
- }
- return result;
- }
-
- @Override
- public int hashCode() {
- final int prime = 37;
- int result = 17;
- if (first != null) {
- result = prime * result + first.hashCode();
- } else {
- result = prime * result;
- }
- if (second != null) {
- result = prime * result + second.hashCode();
- } else {
- result = prime * result;
- }
- return result;
- }
-
- /**
- * @use getFirst()
- */
- @Deprecated
- public T getKey() {
- return first;
- }
-
- /**
- * @use getSecond()
- */
- @Deprecated
- public K getValue() {
- return second;
- }
-
- /**
- * @use setFirst(T key)
- */
- @Deprecated
- public void setKey(T key) {
- this.first = key;
- }
-
- /**
- * @use setSecond(T key)
- */
- @Deprecated
- public void setValue(K value) {
- this.second = value;
- }
-}
+/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.type; + +import java.io.Serializable; + +/** + * @author Roberto E. Escobar + */ +public class Pair<T, K> implements Serializable { + private static final long serialVersionUID = 1764353834209869140L; + private T first; + private K second; + + public Pair(T first, K second) { + this.first = first; + this.second = second; + } + + public T getFirst() { + return first; + } + + public K getSecond() { + return second; + } + + public void setFirst(T key) { + this.first = key; + } + + public void setSecond(K value) { + this.second = value; + } + + public Pair<T, K> set(T first, K second) { + this.first = first; + this.second = second; + return this; + } + + @Override + public String toString() { + String firstString = String.valueOf(first); + String secondString = String.valueOf(second); + return String.format("[%s, %s]", firstString, secondString); + } + + @Override + public boolean equals(Object obj) { + boolean result = false; + if (obj instanceof Pair<?, ?>) { + Pair<?, ?> other = (Pair<?, ?>) obj; + boolean left = first == null ? other.first == null : first.equals(other.first); + boolean right = second == null ? other.second == null : second.equals(other.second); + result = left && right; + } + return result; + } + + @Override + public int hashCode() { + final int prime = 37; + int result = 17; + if (first != null) { + result = prime * result + first.hashCode(); + } else { + result = prime * result; + } + if (second != null) { + result = prime * result + second.hashCode(); + } else { + result = prime * result; + } + return result; + } + + /** + * @use getFirst() + */ + @Deprecated + public T getKey() { + return first; + } + + /** + * @use getSecond() + */ + @Deprecated + public K getValue() { + return second; + } + + /** + * @use setFirst(T key) + */ + @Deprecated + public void setKey(T key) { + this.first = key; + } + + /** + * @use setSecond(T key) + */ + @Deprecated + public void setValue(K value) { + this.second = value; + } +} diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/PropertyStore.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/PropertyStore.java index c244c491770..7575cd736a8 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/PropertyStore.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/PropertyStore.java @@ -1,321 +1,321 @@ -/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.framework.jdk.core.type;
-
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Reader;
-import java.io.Serializable;
-import java.io.StringReader;
-import java.io.StringWriter;
-import java.io.Writer;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Properties;
-import java.util.Set;
-import java.util.TreeSet;
-import java.util.Map.Entry;
-import org.eclipse.osee.framework.jdk.core.util.Collections;
-import org.eclipse.osee.framework.jdk.core.util.Strings;
-
-/**
- * @author Roberto E. Escobar
- */
-public class PropertyStore implements IPropertyStore, Serializable {
- private static final String EMPTY_STRING = new String();
- private static final String[] EMPTY_STRING_ARRAY = new String[0];
-
- private static final long serialVersionUID = 9076969425223251739L;
-
- private static final String EXCEPTION_MESSAGE = "No setting found for key: [%s]";
-
- private String storeId;
- private final Properties storageData;
- private final Properties storageArrays;
- private final Properties storageProperties;
-
- private PropertyStore(String storeId, Properties storageData, Properties storageArrays, Properties storageProperties) {
- super();
- this.storeId = storeId;
- this.storageData = storageData;
- this.storageArrays = storageArrays;
- this.storageProperties = storageProperties;
- }
-
- public PropertyStore(String storeId) {
- this(storeId, new Properties(), new Properties(), new Properties());
- }
-
- public PropertyStore() {
- this(EMPTY_STRING);
- }
-
- public PropertyStore(Properties properties) {
- this(Integer.toString(properties.hashCode()), properties, new Properties(), new Properties());
- }
-
- public String get(String key) {
- return storageData.getProperty(key, EMPTY_STRING);
- }
-
- public String[] getArray(String key) {
- String[] value = (String[]) storageArrays.get(key);
- if (value == null) {
- value = EMPTY_STRING_ARRAY;
- }
- return value;
- }
-
- public boolean getBoolean(String key) {
- return Boolean.valueOf(get(key));
- }
-
- public double getDouble(String key) throws NumberFormatException {
- String setting = get(key);
- if (!Strings.isValid(setting)) {
- throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key));
- }
-
- return new Double(setting).doubleValue();
- }
-
- public float getFloat(String key) throws NumberFormatException {
- String setting = get(key);
- if (!Strings.isValid(setting)) {
- throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key));
- }
-
- return new Float(setting).floatValue();
- }
-
- public int getInt(String key) throws NumberFormatException {
- String setting = get(key);
- if (!Strings.isValid(setting)) {
- throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key));
- }
-
- return new Integer(setting).intValue();
- }
-
- public long getLong(String key) throws NumberFormatException {
- String setting = get(key);
- if (!Strings.isValid(setting)) {
- throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key));
- }
-
- return new Long(setting).longValue();
- }
-
- public PropertyStore getPropertyStore(String key) {
- return (PropertyStore) storageProperties.get(key);
- }
-
- public void put(String key, PropertyStore store) {
- if (store == null) {
- storageProperties.remove(key);
- } else {
- storageProperties.put(key, store);
- }
- }
-
- public void put(String key, String[] value) {
- if (value == null) {
- value = EMPTY_STRING_ARRAY;
- }
- storageArrays.put(key, value);
- }
-
- public void put(String key, double value) {
- put(key, String.valueOf(value));
- }
-
- public void put(String key, float value) {
- put(key, String.valueOf(value));
- }
-
- public void put(String key, int value) {
- put(key, String.valueOf(value));
- }
-
- public void put(String key, long value) {
- put(key, String.valueOf(value));
- }
-
- public void put(String key, String value) {
- if (value == null) {
- value = EMPTY_STRING;
- }
- storageData.setProperty(key, value);
- }
-
- public void put(String key, boolean value) {
- put(key, String.valueOf(value));
- }
-
- public String getId() {
- return storeId;
- }
-
- protected void setId(String name) {
- if (name == null) {
- name = EMPTY_STRING;
- }
- this.storeId = name;
- }
-
- protected Properties getItems() {
- return storageData;
- }
-
- protected Properties getArrays() {
- return storageArrays;
- }
-
- protected Properties getPropertyStores() {
- return storageProperties;
- }
-
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder();
- builder.append(String.format("Id:[%s] Data:%s Arrays:{", getId(), storageData.toString()));
-
- int cnt = 0;
- for (Object key : new TreeSet<Object>(storageArrays.keySet())) {
- if (cnt != 0) {
- builder.append(" ");
- }
- builder.append(key);
- builder.append("=");
- builder.append(Arrays.deepToString((String[]) storageArrays.get(key)));
- cnt++;
- if (cnt < storageArrays.size()) {
- builder.append(",");
- }
- }
- builder.append("}");
- return builder.toString();
- }
-
- public void load(String input) throws Exception {
- PropertyStoreWriter storeWriter = new PropertyStoreWriter();
- storeWriter.load(this, new StringReader(input));
- }
-
- public void load(InputStream inputStream) throws Exception {
- PropertyStoreWriter storeWriter = new PropertyStoreWriter();
- storeWriter.load(this, inputStream);
- }
-
- public void load(Reader reader) throws Exception {
- PropertyStoreWriter storeWriter = new PropertyStoreWriter();
- storeWriter.load(this, reader);
- }
-
- public String save() throws Exception {
- StringWriter writer = new StringWriter();
- save(writer);
- return writer.toString();
- }
-
- public void save(Writer writer) throws Exception {
- PropertyStoreWriter storeWriter = new PropertyStoreWriter();
- storeWriter.save(this, writer);
- }
-
- public void save(OutputStream outputStream) throws Exception {
- PropertyStoreWriter storeWriter = new PropertyStoreWriter();
- storeWriter.save(this, outputStream);
- }
-
- @SuppressWarnings("unchecked")
- public Set<String> arrayKeySet() {
- List<String> items = Collections.castAll(this.storageArrays.keySet());
- return Collections.toSet(items);
- }
-
- @SuppressWarnings("unchecked")
- public Set<String> keySet() {
- List<String> items = Collections.castAll(this.storageData.keySet());
- return Collections.toSet(items);
- }
-
- @SuppressWarnings("unchecked")
- public Set<String> innerStoresKeySet() {
- List<String> items = Collections.castAll(this.storageProperties.keySet());
- return Collections.toSet(items);
- }
-
- @Override
- public boolean equals(Object obj) {
- if (obj instanceof PropertyStore) {
- PropertyStore castObj = (PropertyStore) obj;
- return castObj.storeId.equals(storeId) && castObj.storageData.equals(storageData) && areStorageArraysEqual(castObj);
- }
- return false;
- }
-
- private boolean areStorageArraysEqual(PropertyStore other) {
- boolean result = other.storageArrays.size() == storageArrays.size();
- if (result) {
- for (Entry<Object, Object> expectedEntry : storageArrays.entrySet()) {
- Object expectedValue = expectedEntry.getValue();
- Object actualValue = other.storageArrays.get(expectedEntry.getKey());
- String[] expArray = (String[]) expectedValue;
- String[] actualArray = (String[]) actualValue;
- result &= checkArrays(expArray, actualArray);
- if (!result) {
- break;
- }
- }
- }
- return result;
- }
-
- private boolean checkArrays(String[] expArray, String[] actualArray) {
- boolean result = expArray.length == actualArray.length;
- if (result) {
- for (int index = 0; index < expArray.length; index++) {
- result &= expArray[index].equals(actualArray[index]);
- }
- }
- return result;
- }
-
- @Override
- public int hashCode() {
- int result = 17;
- result = 31 * result + storeId.hashCode();
- result = 31 * result + getPropertiesHashCode(storageData);
- result = 31 * result + getPropertiesHashCode(storageArrays);
- return result;
- }
-
- public int getPropertiesHashCode(Properties properties) {
- int result = 0;
- for (Entry<Object, Object> entry : properties.entrySet()) {
- result += entry.getKey().hashCode();
- Object value = entry.getValue();
- if (value instanceof String[]) {
- result += Arrays.deepHashCode((String[]) value);
- } else {
- result += value.hashCode();
- }
- }
- return result;
- }
-
- @Override
- public boolean isEmpty() {
- return keySet().isEmpty() && innerStoresKeySet().isEmpty() && arrayKeySet().isEmpty();
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.type; + +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Serializable; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; +import java.util.Set; +import java.util.TreeSet; +import java.util.Map.Entry; +import org.eclipse.osee.framework.jdk.core.util.Collections; +import org.eclipse.osee.framework.jdk.core.util.Strings; + +/** + * @author Roberto E. Escobar + */ +public class PropertyStore implements IPropertyStore, Serializable { + private static final String EMPTY_STRING = new String(); + private static final String[] EMPTY_STRING_ARRAY = new String[0]; + + private static final long serialVersionUID = 9076969425223251739L; + + private static final String EXCEPTION_MESSAGE = "No setting found for key: [%s]"; + + private String storeId; + private final Properties storageData; + private final Properties storageArrays; + private final Properties storageProperties; + + private PropertyStore(String storeId, Properties storageData, Properties storageArrays, Properties storageProperties) { + super(); + this.storeId = storeId; + this.storageData = storageData; + this.storageArrays = storageArrays; + this.storageProperties = storageProperties; + } + + public PropertyStore(String storeId) { + this(storeId, new Properties(), new Properties(), new Properties()); + } + + public PropertyStore() { + this(EMPTY_STRING); + } + + public PropertyStore(Properties properties) { + this(Integer.toString(properties.hashCode()), properties, new Properties(), new Properties()); + } + + public String get(String key) { + return storageData.getProperty(key, EMPTY_STRING); + } + + public String[] getArray(String key) { + String[] value = (String[]) storageArrays.get(key); + if (value == null) { + value = EMPTY_STRING_ARRAY; + } + return value; + } + + public boolean getBoolean(String key) { + return Boolean.valueOf(get(key)); + } + + public double getDouble(String key) throws NumberFormatException { + String setting = get(key); + if (!Strings.isValid(setting)) { + throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key)); + } + + return new Double(setting).doubleValue(); + } + + public float getFloat(String key) throws NumberFormatException { + String setting = get(key); + if (!Strings.isValid(setting)) { + throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key)); + } + + return new Float(setting).floatValue(); + } + + public int getInt(String key) throws NumberFormatException { + String setting = get(key); + if (!Strings.isValid(setting)) { + throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key)); + } + + return new Integer(setting).intValue(); + } + + public long getLong(String key) throws NumberFormatException { + String setting = get(key); + if (!Strings.isValid(setting)) { + throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key)); + } + + return new Long(setting).longValue(); + } + + public PropertyStore getPropertyStore(String key) { + return (PropertyStore) storageProperties.get(key); + } + + public void put(String key, PropertyStore store) { + if (store == null) { + storageProperties.remove(key); + } else { + storageProperties.put(key, store); + } + } + + public void put(String key, String[] value) { + if (value == null) { + value = EMPTY_STRING_ARRAY; + } + storageArrays.put(key, value); + } + + public void put(String key, double value) { + put(key, String.valueOf(value)); + } + + public void put(String key, float value) { + put(key, String.valueOf(value)); + } + + public void put(String key, int value) { + put(key, String.valueOf(value)); + } + + public void put(String key, long value) { + put(key, String.valueOf(value)); + } + + public void put(String key, String value) { + if (value == null) { + value = EMPTY_STRING; + } + storageData.setProperty(key, value); + } + + public void put(String key, boolean value) { + put(key, String.valueOf(value)); + } + + public String getId() { + return storeId; + } + + protected void setId(String name) { + if (name == null) { + name = EMPTY_STRING; + } + this.storeId = name; + } + + protected Properties getItems() { + return storageData; + } + + protected Properties getArrays() { + return storageArrays; + } + + protected Properties getPropertyStores() { + return storageProperties; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append(String.format("Id:[%s] Data:%s Arrays:{", getId(), storageData.toString())); + + int cnt = 0; + for (Object key : new TreeSet<Object>(storageArrays.keySet())) { + if (cnt != 0) { + builder.append(" "); + } + builder.append(key); + builder.append("="); + builder.append(Arrays.deepToString((String[]) storageArrays.get(key))); + cnt++; + if (cnt < storageArrays.size()) { + builder.append(","); + } + } + builder.append("}"); + return builder.toString(); + } + + public void load(String input) throws Exception { + PropertyStoreWriter storeWriter = new PropertyStoreWriter(); + storeWriter.load(this, new StringReader(input)); + } + + public void load(InputStream inputStream) throws Exception { + PropertyStoreWriter storeWriter = new PropertyStoreWriter(); + storeWriter.load(this, inputStream); + } + + public void load(Reader reader) throws Exception { + PropertyStoreWriter storeWriter = new PropertyStoreWriter(); + storeWriter.load(this, reader); + } + + public String save() throws Exception { + StringWriter writer = new StringWriter(); + save(writer); + return writer.toString(); + } + + public void save(Writer writer) throws Exception { + PropertyStoreWriter storeWriter = new PropertyStoreWriter(); + storeWriter.save(this, writer); + } + + public void save(OutputStream outputStream) throws Exception { + PropertyStoreWriter storeWriter = new PropertyStoreWriter(); + storeWriter.save(this, outputStream); + } + + @SuppressWarnings("unchecked") + public Set<String> arrayKeySet() { + List<String> items = Collections.castAll(this.storageArrays.keySet()); + return Collections.toSet(items); + } + + @SuppressWarnings("unchecked") + public Set<String> keySet() { + List<String> items = Collections.castAll(this.storageData.keySet()); + return Collections.toSet(items); + } + + @SuppressWarnings("unchecked") + public Set<String> innerStoresKeySet() { + List<String> items = Collections.castAll(this.storageProperties.keySet()); + return Collections.toSet(items); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof PropertyStore) { + PropertyStore castObj = (PropertyStore) obj; + return castObj.storeId.equals(storeId) && castObj.storageData.equals(storageData) && areStorageArraysEqual(castObj); + } + return false; + } + + private boolean areStorageArraysEqual(PropertyStore other) { + boolean result = other.storageArrays.size() == storageArrays.size(); + if (result) { + for (Entry<Object, Object> expectedEntry : storageArrays.entrySet()) { + Object expectedValue = expectedEntry.getValue(); + Object actualValue = other.storageArrays.get(expectedEntry.getKey()); + String[] expArray = (String[]) expectedValue; + String[] actualArray = (String[]) actualValue; + result &= checkArrays(expArray, actualArray); + if (!result) { + break; + } + } + } + return result; + } + + private boolean checkArrays(String[] expArray, String[] actualArray) { + boolean result = expArray.length == actualArray.length; + if (result) { + for (int index = 0; index < expArray.length; index++) { + result &= expArray[index].equals(actualArray[index]); + } + } + return result; + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + storeId.hashCode(); + result = 31 * result + getPropertiesHashCode(storageData); + result = 31 * result + getPropertiesHashCode(storageArrays); + return result; + } + + public int getPropertiesHashCode(Properties properties) { + int result = 0; + for (Entry<Object, Object> entry : properties.entrySet()) { + result += entry.getKey().hashCode(); + Object value = entry.getValue(); + if (value instanceof String[]) { + result += Arrays.deepHashCode((String[]) value); + } else { + result += value.hashCode(); + } + } + return result; + } + + @Override + public boolean isEmpty() { + return keySet().isEmpty() && innerStoresKeySet().isEmpty() && arrayKeySet().isEmpty(); + } + +} diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/PropertyStoreWriter.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/PropertyStoreWriter.java index 3d13feed09f..634a612d672 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/PropertyStoreWriter.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/PropertyStoreWriter.java @@ -1,348 +1,348 @@ -/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.framework.jdk.core.type;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.PrintWriter;
-import java.io.Reader;
-import java.io.UnsupportedEncodingException;
-import java.io.Writer;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Stack;
-import java.util.Map.Entry;
-import javax.xml.stream.XMLInputFactory;
-import javax.xml.stream.XMLStreamConstants;
-import javax.xml.stream.XMLStreamReader;
-import org.eclipse.osee.framework.jdk.core.util.Strings;
-
-public class PropertyStoreWriter {
- private static final String TAG_SECTION = "store";
- private static final String TAG_NAME = "id";
- private static final String TAG_KEY = "key";
- private static final String TAG_VALUE = "value";
- private static final String TAG_LIST = "list";
- private static final String TAG_ITEM = "item";
- private static final String TAG_INNER = "inner.store";
-
- public void load(PropertyStore store, Reader reader) throws Exception {
- XMLReader xmlReader = new XMLReader();
- xmlReader.load(store, reader);
- }
-
- public void load(PropertyStore store, InputStream inputStream) throws Exception {
- load(store, new BufferedReader(new InputStreamReader(inputStream, "utf-8")));
- }
-
- public void save(PropertyStore store, OutputStream stream) throws IOException {
- XMLWriter writer = new XMLWriter(stream);
- internalSave(store, writer);
- writer.close();
- }
-
- public void save(PropertyStore store, Writer writer) throws IOException {
- XMLWriter internalWriter = new XMLWriter(writer);
- internalSave(store, internalWriter);
- }
-
- private void internalSave(PropertyStore store, XMLWriter out) {
- Map<String, String> attributes = new HashMap<String, String>(2);
- String name = store.getId();
- attributes.put(TAG_NAME, name == null ? "" : name);
- out.startTag(TAG_SECTION, attributes);
- attributes.clear();
-
- Properties items = store.getItems();
- for (Entry<Object, Object> entry : items.entrySet()) {
- String key = (String) entry.getKey();
- attributes.put(TAG_KEY, key == null ? "" : key);
- String value = (String) entry.getValue();
- attributes.put(TAG_VALUE, value == null ? "" : value);
- out.printTag(TAG_ITEM, attributes, true);
- }
-
- attributes.clear();
- Properties arrayItems = store.getArrays();
- for (Entry<Object, Object> entry : arrayItems.entrySet()) {
- String key = (String) entry.getKey();
- attributes.put(TAG_KEY, key == null ? "" : key);
- out.startTag(TAG_LIST, attributes);
-
- String[] value = (String[]) entry.getValue();
- attributes.clear();
- if (value != null) {
- for (int index = 0; index < value.length; index++) {
- String item = value[index];
- attributes.put(TAG_VALUE, item == null ? "" : item);
- out.printTag(TAG_ITEM, attributes, true);
- }
- }
- out.endTag(TAG_LIST);
- attributes.clear();
- }
- attributes.clear();
-
- processInnerStores(store, out);
-
- out.endTag(TAG_SECTION);
- }
-
- private void processInnerStores(PropertyStore store, XMLWriter out) {
- Map<String, String> attributes = new HashMap<String, String>(2);
- Properties properties = store.getPropertyStores();
- for (Entry<Object, Object> entry : properties.entrySet()) {
- String key = (String) entry.getKey();
- attributes.put(TAG_KEY, key == null ? "" : key);
- out.startTag(TAG_INNER, attributes);
- PropertyStore innerStore = (PropertyStore) entry.getValue();
- internalSave(innerStore, out);
- out.endTag(TAG_INNER);
- }
- }
-
- private static class XMLReader {
- private static final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
- private List<String> valueList;
- private String tagListKey;
- private boolean isInTagList;
- private final Stack<Pair<String, PropertyStore>> innerStoreStack;
- private String name;
- private String uri;
-
- public XMLReader() {
- isInTagList = false;
- valueList = null;
- tagListKey = null;
- innerStoreStack = new Stack<Pair<String, PropertyStore>>();
- }
-
- private boolean isInNestedStore() {
- return !innerStoreStack.isEmpty();
- }
-
- private PropertyStore getCurrentInnerStore() {
- return innerStoreStack.peek().getSecond();
- }
-
- public void load(PropertyStore store, Reader input) throws Exception {
- try {
- XMLStreamReader streamReader = inputFactory.createXMLStreamReader(input);
- while (streamReader.hasNext()) {
- process(store, streamReader);
- streamReader.next();
- }
- } finally {
- if (input != null) {
- input.close();
- }
- }
- }
-
- private void process(PropertyStore store, XMLStreamReader reader) {
-
- int eventType = reader.getEventType();
- switch (eventType) {
- case XMLStreamConstants.START_ELEMENT:
- name = reader.getLocalName();
- uri = reader.getNamespaceURI();
- if (TAG_SECTION.equals(name)) {
- if (isInNestedStore()) {
- getCurrentInnerStore().setId(reader.getAttributeValue(uri, TAG_NAME));
- } else {
- store.setId(reader.getAttributeValue(uri, TAG_NAME));
- }
- } else if (TAG_ITEM.equals(name)) {
- if (isInNestedStore()) {
- processTagItemSection(uri, getCurrentInnerStore(), reader);
- } else {
- processTagItemSection(uri, store, reader);
- }
- } else if (TAG_LIST.equals(name)) {
- isInTagList = true;
- tagListKey = reader.getAttributeValue(uri, TAG_KEY);
- } else if (TAG_INNER.equals(name)) {
- String key = reader.getAttributeValue(uri, TAG_KEY);
- innerStoreStack.add(new Pair<String, PropertyStore>(key, new PropertyStore()));
- }
- break;
- case XMLStreamConstants.END_ELEMENT:
- name = reader.getLocalName();
- uri = reader.getNamespaceURI();
- if (TAG_LIST.equals(name)) {
- isInTagList = false;
- if (Strings.isValid(tagListKey) && valueList != null && !valueList.isEmpty()) {
- String[] value = valueList.toArray(new String[valueList.size()]);
- if (isInNestedStore()) {
- getCurrentInnerStore().put(tagListKey, value);
- } else {
- store.put(tagListKey, value);
- }
- }
- valueList = null;
- tagListKey = null;
- } else if (TAG_INNER.equals(name)) {
- Pair<String, PropertyStore> completedPair = innerStoreStack.pop();
- String completedKey = completedPair.getFirst();
- PropertyStore completedStore = completedPair.getSecond();
- if (isInNestedStore()) {
- getCurrentInnerStore().put(completedKey, completedStore);
- } else {
- store.put(completedKey, completedStore);
- }
- }
- break;
- case XMLStreamConstants.ENTITY_REFERENCE:
- name = reader.getLocalName();
- uri = reader.getNamespaceURI();
- break;
- default:
- break;
- }
- }
-
- private void processTagItemSection(String uri, PropertyStore store, XMLStreamReader reader) {
- String value = reader.getAttributeValue(uri, TAG_VALUE);
- if (isInTagList) {
- if (valueList == null) {
- valueList = new ArrayList<String>();
- }
- valueList.add(value);
- } else {
- String key = reader.getAttributeValue(uri, TAG_KEY);
- if (Strings.isValid(key)) {
- store.put(key, value);
- }
- }
- }
- }
-
- private static class XMLWriter extends PrintWriter {
- private static final String XML_VERSION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$
- private int tab;
-
- public XMLWriter(Writer writer) {
- super(writer);
- tab = 0;
- println(XML_VERSION);
- }
-
- public XMLWriter(OutputStream output) throws UnsupportedEncodingException {
- this(new OutputStreamWriter(output, "UTF8"));
- }
-
- public void endTag(String name) {
- tab--;
- printTag("/" + name, null, false);
- }
-
- private void printTabulation() {
- for (int i = 0; i < tab; i++) {
- super.print('\t');
- }
- }
-
- public void printTag(String name, Map<String, String> parameters, boolean close) {
- printTag(name, parameters, false, false, close);
- }
-
- private void printTag(String name, Map<String, String> parameters, boolean shouldTab, boolean newLine, boolean close) {
- StringBuffer sb = new StringBuffer();
- sb.append('<');
- sb.append(name);
- if (parameters != null) {
- for (Enumeration<String> e = Collections.enumeration(parameters.keySet()); e.hasMoreElements();) {
- sb.append(" ");
- String key = e.nextElement();
- sb.append(key);
- sb.append("=\"");
- sb.append(getEscaped(String.valueOf(parameters.get(key))));
- sb.append("\"");
- }
- }
- if (close) {
- sb.append('/');
- }
- sb.append('>');
- if (shouldTab) {
- printTabulation();
- }
- if (newLine) {
- println(sb.toString());
- } else {
- print(sb.toString());
- }
- }
-
- public void startTag(String name, Map<String, String> parameters) {
- startTag(name, parameters, true);
- tab++;
- }
-
- private void startTag(String name, Map<String, String> parameters, boolean newLine) {
- printTag(name, parameters, true, newLine, false);
- }
-
- private static void appendEscapedChar(StringBuffer buffer, char c) {
- String replacement = getReplacement(c);
- if (replacement != null) {
- buffer.append('&');
- buffer.append(replacement);
- buffer.append(';');
- } else {
- buffer.append(c);
- }
- }
-
- private static String getEscaped(String s) {
- StringBuffer result = new StringBuffer(s.length() + 10);
- for (int i = 0; i < s.length(); ++i) {
- appendEscapedChar(result, s.charAt(i));
- }
- return result.toString();
- }
-
- private static String getReplacement(char character) {
- // Encode special XML characters into the equivalent character references.
- // The first five are defined by default for all XML documents.
- // The next three (#xD, #xA, #x9) are encoded to avoid them
- // being converted to spaces on deserialization
- switch (character) {
- case '<':
- return "lt";
- case '>':
- return "gt";
- case '"':
- return "quot";
- case '\'':
- return "apos";
- case '&':
- return "amp";
- case '\r':
- return "#x0D";
- case '\n':
- return "#x0A";
- case '\u0009':
- return "#x09";
- }
- return null;
- }
- }
-}
+/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.type; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.Reader; +import java.io.UnsupportedEncodingException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Stack; +import java.util.Map.Entry; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamReader; +import org.eclipse.osee.framework.jdk.core.util.Strings; + +public class PropertyStoreWriter { + private static final String TAG_SECTION = "store"; + private static final String TAG_NAME = "id"; + private static final String TAG_KEY = "key"; + private static final String TAG_VALUE = "value"; + private static final String TAG_LIST = "list"; + private static final String TAG_ITEM = "item"; + private static final String TAG_INNER = "inner.store"; + + public void load(PropertyStore store, Reader reader) throws Exception { + XMLReader xmlReader = new XMLReader(); + xmlReader.load(store, reader); + } + + public void load(PropertyStore store, InputStream inputStream) throws Exception { + load(store, new BufferedReader(new InputStreamReader(inputStream, "utf-8"))); + } + + public void save(PropertyStore store, OutputStream stream) throws IOException { + XMLWriter writer = new XMLWriter(stream); + internalSave(store, writer); + writer.close(); + } + + public void save(PropertyStore store, Writer writer) throws IOException { + XMLWriter internalWriter = new XMLWriter(writer); + internalSave(store, internalWriter); + } + + private void internalSave(PropertyStore store, XMLWriter out) { + Map<String, String> attributes = new HashMap<String, String>(2); + String name = store.getId(); + attributes.put(TAG_NAME, name == null ? "" : name); + out.startTag(TAG_SECTION, attributes); + attributes.clear(); + + Properties items = store.getItems(); + for (Entry<Object, Object> entry : items.entrySet()) { + String key = (String) entry.getKey(); + attributes.put(TAG_KEY, key == null ? "" : key); + String value = (String) entry.getValue(); + attributes.put(TAG_VALUE, value == null ? "" : value); + out.printTag(TAG_ITEM, attributes, true); + } + + attributes.clear(); + Properties arrayItems = store.getArrays(); + for (Entry<Object, Object> entry : arrayItems.entrySet()) { + String key = (String) entry.getKey(); + attributes.put(TAG_KEY, key == null ? "" : key); + out.startTag(TAG_LIST, attributes); + + String[] value = (String[]) entry.getValue(); + attributes.clear(); + if (value != null) { + for (int index = 0; index < value.length; index++) { + String item = value[index]; + attributes.put(TAG_VALUE, item == null ? "" : item); + out.printTag(TAG_ITEM, attributes, true); + } + } + out.endTag(TAG_LIST); + attributes.clear(); + } + attributes.clear(); + + processInnerStores(store, out); + + out.endTag(TAG_SECTION); + } + + private void processInnerStores(PropertyStore store, XMLWriter out) { + Map<String, String> attributes = new HashMap<String, String>(2); + Properties properties = store.getPropertyStores(); + for (Entry<Object, Object> entry : properties.entrySet()) { + String key = (String) entry.getKey(); + attributes.put(TAG_KEY, key == null ? "" : key); + out.startTag(TAG_INNER, attributes); + PropertyStore innerStore = (PropertyStore) entry.getValue(); + internalSave(innerStore, out); + out.endTag(TAG_INNER); + } + } + + private static class XMLReader { + private static final XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + private List<String> valueList; + private String tagListKey; + private boolean isInTagList; + private final Stack<Pair<String, PropertyStore>> innerStoreStack; + private String name; + private String uri; + + public XMLReader() { + isInTagList = false; + valueList = null; + tagListKey = null; + innerStoreStack = new Stack<Pair<String, PropertyStore>>(); + } + + private boolean isInNestedStore() { + return !innerStoreStack.isEmpty(); + } + + private PropertyStore getCurrentInnerStore() { + return innerStoreStack.peek().getSecond(); + } + + public void load(PropertyStore store, Reader input) throws Exception { + try { + XMLStreamReader streamReader = inputFactory.createXMLStreamReader(input); + while (streamReader.hasNext()) { + process(store, streamReader); + streamReader.next(); + } + } finally { + if (input != null) { + input.close(); + } + } + } + + private void process(PropertyStore store, XMLStreamReader reader) { + + int eventType = reader.getEventType(); + switch (eventType) { + case XMLStreamConstants.START_ELEMENT: + name = reader.getLocalName(); + uri = reader.getNamespaceURI(); + if (TAG_SECTION.equals(name)) { + if (isInNestedStore()) { + getCurrentInnerStore().setId(reader.getAttributeValue(uri, TAG_NAME)); + } else { + store.setId(reader.getAttributeValue(uri, TAG_NAME)); + } + } else if (TAG_ITEM.equals(name)) { + if (isInNestedStore()) { + processTagItemSection(uri, getCurrentInnerStore(), reader); + } else { + processTagItemSection(uri, store, reader); + } + } else if (TAG_LIST.equals(name)) { + isInTagList = true; + tagListKey = reader.getAttributeValue(uri, TAG_KEY); + } else if (TAG_INNER.equals(name)) { + String key = reader.getAttributeValue(uri, TAG_KEY); + innerStoreStack.add(new Pair<String, PropertyStore>(key, new PropertyStore())); + } + break; + case XMLStreamConstants.END_ELEMENT: + name = reader.getLocalName(); + uri = reader.getNamespaceURI(); + if (TAG_LIST.equals(name)) { + isInTagList = false; + if (Strings.isValid(tagListKey) && valueList != null && !valueList.isEmpty()) { + String[] value = valueList.toArray(new String[valueList.size()]); + if (isInNestedStore()) { + getCurrentInnerStore().put(tagListKey, value); + } else { + store.put(tagListKey, value); + } + } + valueList = null; + tagListKey = null; + } else if (TAG_INNER.equals(name)) { + Pair<String, PropertyStore> completedPair = innerStoreStack.pop(); + String completedKey = completedPair.getFirst(); + PropertyStore completedStore = completedPair.getSecond(); + if (isInNestedStore()) { + getCurrentInnerStore().put(completedKey, completedStore); + } else { + store.put(completedKey, completedStore); + } + } + break; + case XMLStreamConstants.ENTITY_REFERENCE: + name = reader.getLocalName(); + uri = reader.getNamespaceURI(); + break; + default: + break; + } + } + + private void processTagItemSection(String uri, PropertyStore store, XMLStreamReader reader) { + String value = reader.getAttributeValue(uri, TAG_VALUE); + if (isInTagList) { + if (valueList == null) { + valueList = new ArrayList<String>(); + } + valueList.add(value); + } else { + String key = reader.getAttributeValue(uri, TAG_KEY); + if (Strings.isValid(key)) { + store.put(key, value); + } + } + } + } + + private static class XMLWriter extends PrintWriter { + private static final String XML_VERSION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$ + private int tab; + + public XMLWriter(Writer writer) { + super(writer); + tab = 0; + println(XML_VERSION); + } + + public XMLWriter(OutputStream output) throws UnsupportedEncodingException { + this(new OutputStreamWriter(output, "UTF8")); + } + + public void endTag(String name) { + tab--; + printTag("/" + name, null, false); + } + + private void printTabulation() { + for (int i = 0; i < tab; i++) { + super.print('\t'); + } + } + + public void printTag(String name, Map<String, String> parameters, boolean close) { + printTag(name, parameters, false, false, close); + } + + private void printTag(String name, Map<String, String> parameters, boolean shouldTab, boolean newLine, boolean close) { + StringBuffer sb = new StringBuffer(); + sb.append('<'); + sb.append(name); + if (parameters != null) { + for (Enumeration<String> e = Collections.enumeration(parameters.keySet()); e.hasMoreElements();) { + sb.append(" "); + String key = e.nextElement(); + sb.append(key); + sb.append("=\""); + sb.append(getEscaped(String.valueOf(parameters.get(key)))); + sb.append("\""); + } + } + if (close) { + sb.append('/'); + } + sb.append('>'); + if (shouldTab) { + printTabulation(); + } + if (newLine) { + println(sb.toString()); + } else { + print(sb.toString()); + } + } + + public void startTag(String name, Map<String, String> parameters) { + startTag(name, parameters, true); + tab++; + } + + private void startTag(String name, Map<String, String> parameters, boolean newLine) { + printTag(name, parameters, true, newLine, false); + } + + private static void appendEscapedChar(StringBuffer buffer, char c) { + String replacement = getReplacement(c); + if (replacement != null) { + buffer.append('&'); + buffer.append(replacement); + buffer.append(';'); + } else { + buffer.append(c); + } + } + + private static String getEscaped(String s) { + StringBuffer result = new StringBuffer(s.length() + 10); + for (int i = 0; i < s.length(); ++i) { + appendEscapedChar(result, s.charAt(i)); + } + return result.toString(); + } + + private static String getReplacement(char character) { + // Encode special XML characters into the equivalent character references. + // The first five are defined by default for all XML documents. + // The next three (#xD, #xA, #x9) are encoded to avoid them + // being converted to spaces on deserialization + switch (character) { + case '<': + return "lt"; + case '>': + return "gt"; + case '"': + return "quot"; + case '\'': + return "apos"; + case '&': + return "amp"; + case '\r': + return "#x0D"; + case '\n': + return "#x0A"; + case '\u0009': + return "#x09"; + } + return null; + } + } +} diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/VariantData.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/VariantData.java index c416f5dbd56..f735eb373ac 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/VariantData.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/VariantData.java @@ -1,187 +1,187 @@ -/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.osee.framework.jdk.core.type;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
-import org.eclipse.osee.framework.jdk.core.util.Lib;
-
-/**
- * @author Roberto E. Escobar
- */
-public class VariantData implements IVariantData {
- private static final long serialVersionUID = 9076969425223251739L;
-
- private static final String EXCEPTION_MESSAGE = "No setting found for key: [%s]";
- private static final String CONVERSION_EXCEPTION_MESSAGE = "Unable to convert [%s] from [%s] to [%s]";
-
- private final Properties storageData;
- private final Properties storageArrays;
- private final Map<String, byte[]> byteArrayData;
-
- public VariantData() {
- this.byteArrayData = new HashMap<String, byte[]>();
- this.storageData = new Properties();
- this.storageArrays = new Properties();
- }
-
- public String get(String key) {
- return (String) storageData.get(key);
- }
-
- public boolean isEmpty(String key) {
- if (get(key) == null) {
- return true;
- }
- return get(key).equals("");
- }
-
- public String[] getArray(String key) {
- return (String[]) storageArrays.get(key);
- }
-
- public boolean getBoolean(String key) {
- return Boolean.valueOf((String) storageData.get(key)).booleanValue();
- }
-
- public double getDouble(String key) throws NumberFormatException {
- String setting = (String) storageData.get(key);
- if (setting == null) {
- throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key));
- }
-
- return new Double(setting).doubleValue();
- }
-
- public float getFloat(String key) throws NumberFormatException {
- String setting = (String) storageData.get(key);
- if (setting == null) {
- throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key));
- }
-
- return new Float(setting).floatValue();
- }
-
- public int getInt(String key) throws NumberFormatException {
- String setting = (String) storageData.get(key);
- if (setting == null) {
- throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key));
- }
-
- return new Integer(setting).intValue();
- }
-
- public long getLong(String key) throws NumberFormatException {
- String setting = (String) storageData.get(key);
- if (setting == null) {
- throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key));
- }
- return new Long(setting).longValue();
- }
-
- public Date getDate(String key) throws IllegalArgumentException {
- String setting = (String) storageData.get(key);
- if (setting == null) {
- throw new IllegalArgumentException(String.format(EXCEPTION_MESSAGE, key));
- }
- return new Date(new Long(setting).longValue());
- }
-
- public String getStreamAsString(String key) throws Exception {
- String toReturn;
- InputStream inputStream = getStream(key);
- try {
- toReturn = Lib.inputStreamToString(inputStream);
- } catch (IOException ex) {
- throw new IOException(String.format(CONVERSION_EXCEPTION_MESSAGE, key, "byte[]", "String"));
- }
- return toReturn;
- }
-
- public InputStream getStream(String key) throws IllegalArgumentException {
- byte[] bytes = byteArrayData.get(key);
- if (bytes == null) {
- throw new IllegalArgumentException(String.format(EXCEPTION_MESSAGE, key));
- }
- return new ByteArrayInputStream(bytes);
- }
-
- public void put(String key, String[] value) {
- if (value == null) {
- value = new String[0];
- }
- storageArrays.put(key, value);
- }
-
- public void put(String key, double value) {
- put(key, String.valueOf(value));
- }
-
- public void put(String key, float value) {
- put(key, String.valueOf(value));
- }
-
- public void put(String key, int value) {
- put(key, String.valueOf(value));
- }
-
- public void put(String key, long value) {
- put(key, String.valueOf(value));
- }
-
- public void put(String key, boolean value) {
- put(key, String.valueOf(value));
- }
-
- public void put(String key, Date date) {
- if (date != null) {
- put(key, date.getTime());
- }
- }
-
- public void put(String key, String value) {
- if (value == null) {
- value = "";
- }
- storageData.put(key, value);
- }
-
- public void put(String key, byte[] bytes) {
- if (bytes == null) {
- bytes = new byte[0];
- }
- byteArrayData.put(key, bytes);
- }
-
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder();
- builder.append(storageData.toString().replaceAll(",", ",\n"));
- builder.append(storageArrays.toString().replaceAll(",", ",\n"));
- for (String key : byteArrayData.keySet()) {
- builder.append(key);
- builder.append("=");
- try {
- builder.append(getStreamAsString(key));
- } catch (Exception ex) {
- builder.append("!!Error!!");
- }
- builder.append(",\n");
- }
- return builder.toString();
- }
-}
+/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ + +package org.eclipse.osee.framework.jdk.core.type; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import org.eclipse.osee.framework.jdk.core.util.Lib; + +/** + * @author Roberto E. Escobar + */ +public class VariantData implements IVariantData { + private static final long serialVersionUID = 9076969425223251739L; + + private static final String EXCEPTION_MESSAGE = "No setting found for key: [%s]"; + private static final String CONVERSION_EXCEPTION_MESSAGE = "Unable to convert [%s] from [%s] to [%s]"; + + private final Properties storageData; + private final Properties storageArrays; + private final Map<String, byte[]> byteArrayData; + + public VariantData() { + this.byteArrayData = new HashMap<String, byte[]>(); + this.storageData = new Properties(); + this.storageArrays = new Properties(); + } + + public String get(String key) { + return (String) storageData.get(key); + } + + public boolean isEmpty(String key) { + if (get(key) == null) { + return true; + } + return get(key).equals(""); + } + + public String[] getArray(String key) { + return (String[]) storageArrays.get(key); + } + + public boolean getBoolean(String key) { + return Boolean.valueOf((String) storageData.get(key)).booleanValue(); + } + + public double getDouble(String key) throws NumberFormatException { + String setting = (String) storageData.get(key); + if (setting == null) { + throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key)); + } + + return new Double(setting).doubleValue(); + } + + public float getFloat(String key) throws NumberFormatException { + String setting = (String) storageData.get(key); + if (setting == null) { + throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key)); + } + + return new Float(setting).floatValue(); + } + + public int getInt(String key) throws NumberFormatException { + String setting = (String) storageData.get(key); + if (setting == null) { + throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key)); + } + + return new Integer(setting).intValue(); + } + + public long getLong(String key) throws NumberFormatException { + String setting = (String) storageData.get(key); + if (setting == null) { + throw new NumberFormatException(String.format(EXCEPTION_MESSAGE, key)); + } + return new Long(setting).longValue(); + } + + public Date getDate(String key) throws IllegalArgumentException { + String setting = (String) storageData.get(key); + if (setting == null) { + throw new IllegalArgumentException(String.format(EXCEPTION_MESSAGE, key)); + } + return new Date(new Long(setting).longValue()); + } + + public String getStreamAsString(String key) throws Exception { + String toReturn; + InputStream inputStream = getStream(key); + try { + toReturn = Lib.inputStreamToString(inputStream); + } catch (IOException ex) { + throw new IOException(String.format(CONVERSION_EXCEPTION_MESSAGE, key, "byte[]", "String")); + } + return toReturn; + } + + public InputStream getStream(String key) throws IllegalArgumentException { + byte[] bytes = byteArrayData.get(key); + if (bytes == null) { + throw new IllegalArgumentException(String.format(EXCEPTION_MESSAGE, key)); + } + return new ByteArrayInputStream(bytes); + } + + public void put(String key, String[] value) { + if (value == null) { + value = new String[0]; + } + storageArrays.put(key, value); + } + + public void put(String key, double value) { + put(key, String.valueOf(value)); + } + + public void put(String key, float value) { + put(key, String.valueOf(value)); + } + + public void put(String key, int value) { + put(key, String.valueOf(value)); + } + + public void put(String key, long value) { + put(key, String.valueOf(value)); + } + + public void put(String key, boolean value) { + put(key, String.valueOf(value)); + } + + public void put(String key, Date date) { + if (date != null) { + put(key, date.getTime()); + } + } + + public void put(String key, String value) { + if (value == null) { + value = ""; + } + storageData.put(key, value); + } + + public void put(String key, byte[] bytes) { + if (bytes == null) { + bytes = new byte[0]; + } + byteArrayData.put(key, bytes); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append(storageData.toString().replaceAll(",", ",\n")); + builder.append(storageArrays.toString().replaceAll(",", ",\n")); + for (String key : byteArrayData.keySet()) { + builder.append(key); + builder.append("="); + try { + builder.append(getStreamAsString(key)); + } catch (Exception ex) { + builder.append("!!Error!!"); + } + builder.append(",\n"); + } + return builder.toString(); + } +} |