Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/GenericMapValues.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/GenericMapValues.java?rev=1695126&r1=1695125&r2=1695126&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/GenericMapValues.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/GenericMapValues.java Mon Aug 10 16:15:37 2015 @@ -1,79 +1,79 @@ -/******************************************************************************* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - *******************************************************************************/ -package org.ofbiz.base.util.collections; - -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.ofbiz.base.util.UtilObject; - -public abstract class GenericMapValues<K, V, M extends Map<K, V>> extends GenericMapCollection<K, V, M, V> { - public GenericMapValues(M source) { - super(source); - } - - public boolean contains(Object item) { - Iterator<V> it = iterator(false); - while (it.hasNext()) { - if (UtilObject.equalsHelper(item, it.next())) return true; - } - return false; - } - - @Override - public boolean equals(Object o) { - if (!(o instanceof Collection<?>)) return false; - if (o instanceof List<?> || o instanceof Set<?>) return false; - Collection<?> other = (Collection<?>) o; - if (source.size() != other.size()) return false; - Iterator<V> it = iterator(false); - while (it.hasNext()) { - V item = it.next(); - if (!other.contains(item)) return false; - } - return true; - } - - @Override - public int hashCode() { - int h = 0; - Iterator<V> it = iterator(false); - while (it.hasNext()) { - V item = it.next(); - if (item == null) continue; - h += item.hashCode(); - } - return h; - } - - public boolean remove(Object item) { - Iterator<V> it = iterator(false); - while (it.hasNext()) { - if (UtilObject.equalsHelper(item, it.next())) { - it.remove(); - return true; - } - } - return false; - } -} - +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.base.util.collections; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.ofbiz.base.util.UtilObject; + +public abstract class GenericMapValues<K, V, M extends Map<K, V>> extends GenericMapCollection<K, V, M, V> { + public GenericMapValues(M source) { + super(source); + } + + public boolean contains(Object item) { + Iterator<V> it = iterator(false); + while (it.hasNext()) { + if (UtilObject.equalsHelper(item, it.next())) return true; + } + return false; + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof Collection<?>)) return false; + if (o instanceof List<?> || o instanceof Set<?>) return false; + Collection<?> other = (Collection<?>) o; + if (source.size() != other.size()) return false; + Iterator<V> it = iterator(false); + while (it.hasNext()) { + V item = it.next(); + if (!other.contains(item)) return false; + } + return true; + } + + @Override + public int hashCode() { + int h = 0; + Iterator<V> it = iterator(false); + while (it.hasNext()) { + V item = it.next(); + if (item == null) continue; + h += item.hashCode(); + } + return h; + } + + public boolean remove(Object item) { + Iterator<V> it = iterator(false); + while (it.hasNext()) { + if (UtilObject.equalsHelper(item, it.next())) { + it.remove(); + return true; + } + } + return false; + } +} + Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/GenericMapValues.java ('svn:eol-style' removed) Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/IteratorWrapper.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/IteratorWrapper.java?rev=1695126&r1=1695125&r2=1695126&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/IteratorWrapper.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/IteratorWrapper.java Mon Aug 10 16:15:37 2015 @@ -1,75 +1,75 @@ -/******************************************************************************* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - *******************************************************************************/ -package org.ofbiz.base.util.collections; - -import java.util.Iterator; -import java.util.NoSuchElementException; - -public abstract class IteratorWrapper<DEST, SRC> implements Iterator<DEST> { - private final Iterator<? extends SRC> it; - private boolean nextCalled; - private DEST lastDest; - private SRC lastSrc; - - protected IteratorWrapper(Iterator<? extends SRC> it) { - this.it = it; - } - - public boolean hasNext() { - if (nextCalled) return true; - if (!it.hasNext()) return false; - do { - lastSrc = it.next(); - DEST nextDest = convert(lastSrc); - if (isValid(lastSrc, nextDest)) { - nextCalled = true; - lastDest = nextDest; - return true; - } - } while (it.hasNext()); - return false; - } - - public DEST next() { - if (!nextCalled) { - if (!hasNext()) throw new NoSuchElementException(); - } - nextCalled = false; - return lastDest; - } - - public void remove() { - if (lastSrc != null) { - noteRemoval(lastDest, lastSrc); - it.remove(); - lastDest = null; - lastSrc = null; - } else { - throw new IllegalStateException(); - } - } - - protected boolean isValid(SRC src, DEST dest) { - return true; - } - - protected abstract void noteRemoval(DEST dest, SRC src); - protected abstract DEST convert(SRC src); -} - +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.base.util.collections; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +public abstract class IteratorWrapper<DEST, SRC> implements Iterator<DEST> { + private final Iterator<? extends SRC> it; + private boolean nextCalled; + private DEST lastDest; + private SRC lastSrc; + + protected IteratorWrapper(Iterator<? extends SRC> it) { + this.it = it; + } + + public boolean hasNext() { + if (nextCalled) return true; + if (!it.hasNext()) return false; + do { + lastSrc = it.next(); + DEST nextDest = convert(lastSrc); + if (isValid(lastSrc, nextDest)) { + nextCalled = true; + lastDest = nextDest; + return true; + } + } while (it.hasNext()); + return false; + } + + public DEST next() { + if (!nextCalled) { + if (!hasNext()) throw new NoSuchElementException(); + } + nextCalled = false; + return lastDest; + } + + public void remove() { + if (lastSrc != null) { + noteRemoval(lastDest, lastSrc); + it.remove(); + lastDest = null; + lastSrc = null; + } else { + throw new IllegalStateException(); + } + } + + protected boolean isValid(SRC src, DEST dest) { + return true; + } + + protected abstract void noteRemoval(DEST dest, SRC src); + protected abstract DEST convert(SRC src); +} + Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/IteratorWrapper.java ('svn:eol-style' removed) Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/MapContext.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/MapContext.java?rev=1695126&r1=1695125&r2=1695126&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/MapContext.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/MapContext.java Mon Aug 10 16:15:37 2015 @@ -1,337 +1,337 @@ -/******************************************************************************* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - *******************************************************************************/ -package org.ofbiz.base.util.collections; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; - -import org.ofbiz.base.util.UtilGenerics; - - -/** - * Map Stack - * - */ -public class MapContext<K, V> implements Map<K, V>, LocalizedMap<V> { - - public static final String module = MapContext.class.getName(); - - public static final <K, V> MapContext<K, V> getMapContext() { - return new MapContext<K, V>(); - } - - public static <K, V> MapContext<K, V> createMapContext() { - MapContext<K, V> newValue = MapContext.getMapContext(); - // initialize with a single entry - newValue.push(); - return newValue; - } - - @SuppressWarnings("unchecked") - public static <K, V> MapContext<K, V> createMapContext(Map<K, V> baseMap) { - MapContext<K, V> newValue = MapContext.getMapContext(); - if (baseMap instanceof MapContext) { - newValue.stackList.addAll(((MapContext) baseMap).stackList); - } else { - newValue.stackList.add(0, baseMap); - } - return newValue; - } - - /** Does a shallow copy of the internal stack of the passed MapContext; enables simultaneous stacks that share common parent Maps */ - public static <K, V> MapContext<K, V> createMapContext(MapContext<K, V> source) { - MapContext<K, V> newValue = MapContext.getMapContext(); - newValue.stackList.addAll(source.stackList); - return newValue; - } - - protected MapContext() { - super(); - } - - protected List<Map<K, V>> stackList = new LinkedList<Map<K, V>>(); - - public void reset() { - stackList = new LinkedList<Map<K, V>>(); - } - - /** Puts a new Map on the top of the stack */ - public void push() { - Map<K, V> newMap = new HashMap<K, V>(); - this.stackList.add(0,newMap); - } - - /** Puts an existing Map on the top of the stack (top meaning will override lower layers on the stack) */ - public void push(Map<K, V> existingMap) { - if (existingMap == null) { - throw new IllegalArgumentException("Error: cannot push null existing Map onto a MapContext"); - } - this.stackList.add(0, existingMap); - } - - /** Puts an existing Map on the BOTTOM of the stack (bottom meaning will be overriden by lower layers on the stack, ie everything else already there) */ - public void addToBottom(Map<K, V> existingMap) { - if (existingMap == null) { - throw new IllegalArgumentException("Error: cannot add null existing Map to bottom of a MapContext"); - } - this.stackList.add(existingMap); - } - - /** Remove and returns the Map from the top of the stack; if there is only one Map on the stack it returns null and does not remove it */ - public Map<K, V> pop() { - // always leave at least one Map in the List, ie never pop off the last Map - if (this.stackList.size() > 1) { - return stackList.remove(0); - } else { - return null; - } - } - - /** - * Creates a MapContext object that has the same Map objects on its stack; - * meant to be used to enable a - * situation where a parent and child context are operating simultaneously - * using two different MapContext objects, but sharing the Maps in common - */ - public MapContext<K, V> standAloneStack() { - MapContext<K, V> standAlone = MapContext.createMapContext(this); - return standAlone; - } - - /** - * Creates a MapContext object that has the same Map objects on its stack, - * but with a new Map pushed on the top; meant to be used to enable a - * situation where a parent and child context are operating simultaneously - * using two different MapContext objects, but sharing the Maps in common - */ - public MapContext<K, V> standAloneChildStack() { - MapContext<K, V> standAloneChild = MapContext.createMapContext(this); - standAloneChild.push(); - return standAloneChild; - } - - /* (non-Javadoc) - * @see java.util.Map#size() - */ - public int size() { - // a little bit tricky; to represent the apparent size we need to aggregate all keys and get a count of unique keys - // this is a bit of a slow way, but gets the best number possible - Set<K> keys = this.keySet(); - return keys.size(); - } - - /* (non-Javadoc) - * @see java.util.Map#isEmpty() - */ - public boolean isEmpty() { - // walk the stackList and if any is not empty, return false; otherwise return true - for (Map<K, V> curMap: this.stackList) { - if (!curMap.isEmpty()) { - return false; - } - } - return true; - } - - /* (non-Javadoc) - * @see java.util.Map#containsKey(java.lang.Object) - */ - public boolean containsKey(Object key) { - // walk the stackList and for the first place it is found return true; otherwise refurn false - for (Map<K, V> curMap: this.stackList) { - if (curMap.containsKey(key)) { - return true; - } - } - return false; - } - - /* (non-Javadoc) - * @see java.util.Map#containsValue(java.lang.Object) - */ - public boolean containsValue(Object value) { - // walk the stackList and the entries for each Map and if nothing is in for the current key, consider it an option, otherwise ignore - Set<K> resultKeySet = new HashSet<K>(); - for (Map<K, V> curMap: this.stackList) { - for (Map.Entry<K, V> curEntry: curMap.entrySet()) { - if (!resultKeySet.contains(curEntry.getKey())) { - resultKeySet.add(curEntry.getKey()); - if (value == null) { - if (curEntry.getValue() == null) { - return true; - } - } else { - if (value.equals(curEntry.getValue())) { - return true; - } - } - } - } - } - return false; - } - - /* (non-Javadoc) - * @see java.util.Map#get(java.lang.Object) - */ - public V get(Object key) { - // walk the stackList and for the first place it is found return true; otherwise refurn false - for (Map<K, V> curMap: this.stackList) { - // only return if the curMap contains the key, rather than checking for null; this allows a null at a lower level to override a value at a higher level - if (curMap.containsKey(key)) { - return curMap.get(key); - } - } - return null; - } - - /* (non-Javadoc) - * @see org.ofbiz.base.util.collections.LocalizedMap#get(java.lang.String, java.util.Locale) - */ - public V get(String name, Locale locale) { - // walk the stackList and for the first place it is found return true; otherwise refurn false - for (Map<K, V> curMap: this.stackList) { - // only return if the curMap contains the key, rather than checking for null; this allows a null at a lower level to override a value at a higher level - if (curMap.containsKey(name)) { - if (curMap instanceof LocalizedMap<?>) { - LocalizedMap<V> lmap = UtilGenerics.cast(curMap); - return lmap.get(name, locale); - } else { - return curMap.get(name); - } - } - } - return null; - } - - /* (non-Javadoc) - * @see java.util.Map#put(java.lang.Object, java.lang.Object) - */ - public V put(K key, V value) { - // all write operations are local: only put in the Map on the top of the stack - Map<K, V> currentMap = this.stackList.get(0); - return currentMap.put(key, value); - } - - /* (non-Javadoc) - * @see java.util.Map#remove(java.lang.Object) - */ - public V remove(Object key) { - // all write operations are local: only remove from the Map on the top of the stack - Map<K, V> currentMap = this.stackList.get(0); - return currentMap.remove(key); - } - - /* (non-Javadoc) - * @see java.util.Map#putAll(java.util.Map) - */ - public void putAll(Map<? extends K, ? extends V> arg0) { - // all write operations are local: only put in the Map on the top of the stack - Map<K, V> currentMap = this.stackList.get(0); - currentMap.putAll(arg0); - } - - /* (non-Javadoc) - * @see java.util.Map#clear() - */ - public void clear() { - // all write operations are local: only clear the Map on the top of the stack - this.stackList.get(0).clear(); - } - - /* (non-Javadoc) - * @see java.util.Map#keySet() - */ - public Set<K> keySet() { - // walk the stackList and aggregate all keys - Set<K> resultSet = new HashSet<K>(); - for (Map<K, V> curMap: this.stackList) { - resultSet.addAll(curMap.keySet()); - } - return Collections.unmodifiableSet(resultSet); - } - - /* (non-Javadoc) - * @see java.util.Map#values() - */ - public Collection<V> values() { - // walk the stackList and the entries for each Map and if nothing is in for the current key, put it in - Set<K> resultKeySet = new HashSet<K>(); - List<V> resultValues = new LinkedList<V>(); - for (Map<K, V> curMap: this.stackList) { - for (Map.Entry<K, V> curEntry: curMap.entrySet()) { - if (!resultKeySet.contains(curEntry.getKey())) { - resultKeySet.add(curEntry.getKey()); - resultValues.add(curEntry.getValue()); - } - } - } - return Collections.unmodifiableCollection(resultValues); - } - - /* (non-Javadoc) - * @see java.util.Map#entrySet() - */ - public Set<Map.Entry<K, V>> entrySet() { - // walk the stackList and the entries for each Map and if nothing is in for the current key, put it in - Set<K> resultKeySet = new HashSet<K>(); - Set<Map.Entry<K, V>> resultEntrySet = new HashSet<Map.Entry<K, V>>(); - for (Map<K, V> curMap: this.stackList) { - for (Map.Entry<K, V> curEntry: curMap.entrySet()) { - if (!resultKeySet.contains(curEntry.getKey())) { - resultKeySet.add(curEntry.getKey()); - resultEntrySet.add(curEntry); - } - } - } - return Collections.unmodifiableSet(resultEntrySet); - } - - @Override - public String toString() { - StringBuilder fullMapString = new StringBuilder(); - int curLevel = 0; - for (Map<K, V> curMap: this.stackList) { - fullMapString.append("============================== Start stack level " + curLevel + "\n"); - for (Map.Entry<K, V> curEntry: curMap.entrySet()) { - - fullMapString.append("==>["); - fullMapString.append(curEntry.getKey()); - fullMapString.append("]:"); - // skip the instances of MapContext to avoid infinite loop - if (curEntry.getValue() instanceof MapContext<?, ?>) { - fullMapString.append("<Instance of MapContext, not printing to avoid infinite recursion>"); - } else { - fullMapString.append(curEntry.getValue()); - } - fullMapString.append("\n"); - } - fullMapString.append("============================== End stack level " + curLevel + "\n"); - curLevel++; - } - return fullMapString.toString(); - } -} +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.base.util.collections; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +import org.ofbiz.base.util.UtilGenerics; + + +/** + * Map Stack + * + */ +public class MapContext<K, V> implements Map<K, V>, LocalizedMap<V> { + + public static final String module = MapContext.class.getName(); + + public static final <K, V> MapContext<K, V> getMapContext() { + return new MapContext<K, V>(); + } + + public static <K, V> MapContext<K, V> createMapContext() { + MapContext<K, V> newValue = MapContext.getMapContext(); + // initialize with a single entry + newValue.push(); + return newValue; + } + + @SuppressWarnings("unchecked") + public static <K, V> MapContext<K, V> createMapContext(Map<K, V> baseMap) { + MapContext<K, V> newValue = MapContext.getMapContext(); + if (baseMap instanceof MapContext) { + newValue.stackList.addAll(((MapContext) baseMap).stackList); + } else { + newValue.stackList.add(0, baseMap); + } + return newValue; + } + + /** Does a shallow copy of the internal stack of the passed MapContext; enables simultaneous stacks that share common parent Maps */ + public static <K, V> MapContext<K, V> createMapContext(MapContext<K, V> source) { + MapContext<K, V> newValue = MapContext.getMapContext(); + newValue.stackList.addAll(source.stackList); + return newValue; + } + + protected MapContext() { + super(); + } + + protected List<Map<K, V>> stackList = new LinkedList<Map<K, V>>(); + + public void reset() { + stackList = new LinkedList<Map<K, V>>(); + } + + /** Puts a new Map on the top of the stack */ + public void push() { + Map<K, V> newMap = new HashMap<K, V>(); + this.stackList.add(0,newMap); + } + + /** Puts an existing Map on the top of the stack (top meaning will override lower layers on the stack) */ + public void push(Map<K, V> existingMap) { + if (existingMap == null) { + throw new IllegalArgumentException("Error: cannot push null existing Map onto a MapContext"); + } + this.stackList.add(0, existingMap); + } + + /** Puts an existing Map on the BOTTOM of the stack (bottom meaning will be overriden by lower layers on the stack, ie everything else already there) */ + public void addToBottom(Map<K, V> existingMap) { + if (existingMap == null) { + throw new IllegalArgumentException("Error: cannot add null existing Map to bottom of a MapContext"); + } + this.stackList.add(existingMap); + } + + /** Remove and returns the Map from the top of the stack; if there is only one Map on the stack it returns null and does not remove it */ + public Map<K, V> pop() { + // always leave at least one Map in the List, ie never pop off the last Map + if (this.stackList.size() > 1) { + return stackList.remove(0); + } else { + return null; + } + } + + /** + * Creates a MapContext object that has the same Map objects on its stack; + * meant to be used to enable a + * situation where a parent and child context are operating simultaneously + * using two different MapContext objects, but sharing the Maps in common + */ + public MapContext<K, V> standAloneStack() { + MapContext<K, V> standAlone = MapContext.createMapContext(this); + return standAlone; + } + + /** + * Creates a MapContext object that has the same Map objects on its stack, + * but with a new Map pushed on the top; meant to be used to enable a + * situation where a parent and child context are operating simultaneously + * using two different MapContext objects, but sharing the Maps in common + */ + public MapContext<K, V> standAloneChildStack() { + MapContext<K, V> standAloneChild = MapContext.createMapContext(this); + standAloneChild.push(); + return standAloneChild; + } + + /* (non-Javadoc) + * @see java.util.Map#size() + */ + public int size() { + // a little bit tricky; to represent the apparent size we need to aggregate all keys and get a count of unique keys + // this is a bit of a slow way, but gets the best number possible + Set<K> keys = this.keySet(); + return keys.size(); + } + + /* (non-Javadoc) + * @see java.util.Map#isEmpty() + */ + public boolean isEmpty() { + // walk the stackList and if any is not empty, return false; otherwise return true + for (Map<K, V> curMap: this.stackList) { + if (!curMap.isEmpty()) { + return false; + } + } + return true; + } + + /* (non-Javadoc) + * @see java.util.Map#containsKey(java.lang.Object) + */ + public boolean containsKey(Object key) { + // walk the stackList and for the first place it is found return true; otherwise refurn false + for (Map<K, V> curMap: this.stackList) { + if (curMap.containsKey(key)) { + return true; + } + } + return false; + } + + /* (non-Javadoc) + * @see java.util.Map#containsValue(java.lang.Object) + */ + public boolean containsValue(Object value) { + // walk the stackList and the entries for each Map and if nothing is in for the current key, consider it an option, otherwise ignore + Set<K> resultKeySet = new HashSet<K>(); + for (Map<K, V> curMap: this.stackList) { + for (Map.Entry<K, V> curEntry: curMap.entrySet()) { + if (!resultKeySet.contains(curEntry.getKey())) { + resultKeySet.add(curEntry.getKey()); + if (value == null) { + if (curEntry.getValue() == null) { + return true; + } + } else { + if (value.equals(curEntry.getValue())) { + return true; + } + } + } + } + } + return false; + } + + /* (non-Javadoc) + * @see java.util.Map#get(java.lang.Object) + */ + public V get(Object key) { + // walk the stackList and for the first place it is found return true; otherwise refurn false + for (Map<K, V> curMap: this.stackList) { + // only return if the curMap contains the key, rather than checking for null; this allows a null at a lower level to override a value at a higher level + if (curMap.containsKey(key)) { + return curMap.get(key); + } + } + return null; + } + + /* (non-Javadoc) + * @see org.ofbiz.base.util.collections.LocalizedMap#get(java.lang.String, java.util.Locale) + */ + public V get(String name, Locale locale) { + // walk the stackList and for the first place it is found return true; otherwise refurn false + for (Map<K, V> curMap: this.stackList) { + // only return if the curMap contains the key, rather than checking for null; this allows a null at a lower level to override a value at a higher level + if (curMap.containsKey(name)) { + if (curMap instanceof LocalizedMap<?>) { + LocalizedMap<V> lmap = UtilGenerics.cast(curMap); + return lmap.get(name, locale); + } else { + return curMap.get(name); + } + } + } + return null; + } + + /* (non-Javadoc) + * @see java.util.Map#put(java.lang.Object, java.lang.Object) + */ + public V put(K key, V value) { + // all write operations are local: only put in the Map on the top of the stack + Map<K, V> currentMap = this.stackList.get(0); + return currentMap.put(key, value); + } + + /* (non-Javadoc) + * @see java.util.Map#remove(java.lang.Object) + */ + public V remove(Object key) { + // all write operations are local: only remove from the Map on the top of the stack + Map<K, V> currentMap = this.stackList.get(0); + return currentMap.remove(key); + } + + /* (non-Javadoc) + * @see java.util.Map#putAll(java.util.Map) + */ + public void putAll(Map<? extends K, ? extends V> arg0) { + // all write operations are local: only put in the Map on the top of the stack + Map<K, V> currentMap = this.stackList.get(0); + currentMap.putAll(arg0); + } + + /* (non-Javadoc) + * @see java.util.Map#clear() + */ + public void clear() { + // all write operations are local: only clear the Map on the top of the stack + this.stackList.get(0).clear(); + } + + /* (non-Javadoc) + * @see java.util.Map#keySet() + */ + public Set<K> keySet() { + // walk the stackList and aggregate all keys + Set<K> resultSet = new HashSet<K>(); + for (Map<K, V> curMap: this.stackList) { + resultSet.addAll(curMap.keySet()); + } + return Collections.unmodifiableSet(resultSet); + } + + /* (non-Javadoc) + * @see java.util.Map#values() + */ + public Collection<V> values() { + // walk the stackList and the entries for each Map and if nothing is in for the current key, put it in + Set<K> resultKeySet = new HashSet<K>(); + List<V> resultValues = new LinkedList<V>(); + for (Map<K, V> curMap: this.stackList) { + for (Map.Entry<K, V> curEntry: curMap.entrySet()) { + if (!resultKeySet.contains(curEntry.getKey())) { + resultKeySet.add(curEntry.getKey()); + resultValues.add(curEntry.getValue()); + } + } + } + return Collections.unmodifiableCollection(resultValues); + } + + /* (non-Javadoc) + * @see java.util.Map#entrySet() + */ + public Set<Map.Entry<K, V>> entrySet() { + // walk the stackList and the entries for each Map and if nothing is in for the current key, put it in + Set<K> resultKeySet = new HashSet<K>(); + Set<Map.Entry<K, V>> resultEntrySet = new HashSet<Map.Entry<K, V>>(); + for (Map<K, V> curMap: this.stackList) { + for (Map.Entry<K, V> curEntry: curMap.entrySet()) { + if (!resultKeySet.contains(curEntry.getKey())) { + resultKeySet.add(curEntry.getKey()); + resultEntrySet.add(curEntry); + } + } + } + return Collections.unmodifiableSet(resultEntrySet); + } + + @Override + public String toString() { + StringBuilder fullMapString = new StringBuilder(); + int curLevel = 0; + for (Map<K, V> curMap: this.stackList) { + fullMapString.append("============================== Start stack level " + curLevel + "\n"); + for (Map.Entry<K, V> curEntry: curMap.entrySet()) { + + fullMapString.append("==>["); + fullMapString.append(curEntry.getKey()); + fullMapString.append("]:"); + // skip the instances of MapContext to avoid infinite loop + if (curEntry.getValue() instanceof MapContext<?, ?>) { + fullMapString.append("<Instance of MapContext, not printing to avoid infinite recursion>"); + } else { + fullMapString.append(curEntry.getValue()); + } + fullMapString.append("\n"); + } + fullMapString.append("============================== End stack level " + curLevel + "\n"); + curLevel++; + } + return fullMapString.toString(); + } +} Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/MapContext.java ('svn:eol-style' removed) Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/ReadOnlyMapEntry.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/ReadOnlyMapEntry.java?rev=1695126&r1=1695125&r2=1695126&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/ReadOnlyMapEntry.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/ReadOnlyMapEntry.java Mon Aug 10 16:15:37 2015 @@ -1,61 +1,61 @@ -/******************************************************************************* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - *******************************************************************************/ -package org.ofbiz.base.util.collections; - -import java.util.Map; - -import org.ofbiz.base.util.UtilObject; - -/** this class can go away when ofbiz switches to java 1.6, replaced by - * AbstractMap.SimpleImmutableEntry - */ -public class ReadOnlyMapEntry<K, V> implements Map.Entry<K, V> { - protected final K key; - protected final V value; - - public ReadOnlyMapEntry(K key, V value) { - this.key = key; - this.value = value; - } - - public K getKey() { - return key; - } - - public V getValue() { - return value; - } - - public V setValue(V value) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean equals(Object o) { - if (!(o instanceof Map.Entry<?, ?>)) return false; - if (this == o) return true; - Map.Entry<?, ?> other = (Map.Entry<?, ?>) o; - return UtilObject.equalsHelper(getKey(), other.getKey()) && UtilObject.equalsHelper(getValue(), other.getValue()); - } - - @Override - public int hashCode() { - return UtilObject.doHashCode(getKey()) ^ UtilObject.doHashCode(getValue()); - } -} +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.base.util.collections; + +import java.util.Map; + +import org.ofbiz.base.util.UtilObject; + +/** this class can go away when ofbiz switches to java 1.6, replaced by + * AbstractMap.SimpleImmutableEntry + */ +public class ReadOnlyMapEntry<K, V> implements Map.Entry<K, V> { + protected final K key; + protected final V value; + + public ReadOnlyMapEntry(K key, V value) { + this.key = key; + this.value = value; + } + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } + + public V setValue(V value) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof Map.Entry<?, ?>)) return false; + if (this == o) return true; + Map.Entry<?, ?> other = (Map.Entry<?, ?>) o; + return UtilObject.equalsHelper(getKey(), other.getKey()) && UtilObject.equalsHelper(getValue(), other.getValue()); + } + + @Override + public int hashCode() { + return UtilObject.doHashCode(getKey()) ^ UtilObject.doHashCode(getValue()); + } +} Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/ReadOnlyMapEntry.java ('svn:eol-style' removed) Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/test/FlexibleMapAccessorTests.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/test/FlexibleMapAccessorTests.java?rev=1695126&r1=1695125&r2=1695126&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/test/FlexibleMapAccessorTests.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/test/FlexibleMapAccessorTests.java Mon Aug 10 16:15:37 2015 @@ -1,188 +1,188 @@ -/******************************************************************************* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - *******************************************************************************/ -package org.ofbiz.base.util.collections.test; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Locale; -import java.util.Map; -import java.util.Set; -import java.math.BigDecimal; - -import org.ofbiz.base.lang.SourceMonitored; -import org.ofbiz.base.test.GenericTestCaseBase; -import org.ofbiz.base.util.Debug; -import org.ofbiz.base.util.collections.FlexibleMapAccessor; -import org.ofbiz.base.util.string.FlexibleStringExpander; - -@SourceMonitored -public class FlexibleMapAccessorTests extends GenericTestCaseBase { - private static final Locale localeToTest = new Locale("en", "US"); - private static FlexibleMapAccessor<?> fmaEmpty = FlexibleMapAccessor.getInstance(""); - private static FlexibleMapAccessor<?> fmaNull = FlexibleMapAccessor.getInstance(null); - - public FlexibleMapAccessorTests(String name) { - super(name); - } - - private static <T> void fmaTest(String label, String getText, String fseText, T var, String value) { - fmaTest(label, getText, getText, fseText, null, var, value); - } - - private static <T> void fmaTest(String label, String getText, String putText, String fseText, Locale locale, T var, String value) { - Map<String, Object> testMap = new HashMap<String, Object>(); - FlexibleMapAccessor<T> fmaGet = FlexibleMapAccessor.getInstance(getText); - assertEquals(label + ":get-original-name", getText, fmaGet.getOriginalName()); - assertEquals(label + ":get-isEmpty", false, fmaGet.isEmpty()); - assertEquals(label + ":get-instance-equals", fmaGet, FlexibleMapAccessor.getInstance(getText)); - assertEquals(label + ":toString", getText, fmaGet.toString()); - assertNotEquals(label + ":get-not-equals-empty", fmaEmpty, fmaGet); - assertNotEquals(label + ":get-not-equals-null", fmaNull, fmaGet); - assertNotEquals(label + ":empty-not-equals-get", fmaGet, fmaEmpty); - assertNotEquals(label + ":null-not-equals-get", fmaGet, fmaNull); - assertNotEquals(label + ":get-not-equals-other", fmaGet, FlexibleMapAccessorTests.class); - assertEquals(label + ":get-toString", getText, fmaGet.toString()); - FlexibleMapAccessor<T> fmaGetAscending = FlexibleMapAccessor.getInstance("+" + getText); - assertEquals(label + ":get-ascending-toString", "+" + getText, fmaGetAscending.toString()); - assertTrue(label + ":get-ascending-isAscending", fmaGetAscending.getIsAscending()); - FlexibleMapAccessor<T> fmaGetDescending = FlexibleMapAccessor.getInstance("-" + getText); - assertEquals(label + ":get-descending-toString", "-" + getText, fmaGetDescending.toString()); - assertFalse(label + ":get-decending-isAscending", fmaGetDescending.getIsAscending()); - FlexibleMapAccessor<T> fmaPut = FlexibleMapAccessor.getInstance(putText); - assertEquals(label + ":put-toString", putText, fmaPut.toString()); - assertEquals(label + ":put-original-name", putText, fmaPut.getOriginalName()); - assertEquals(label + ":put-isEmpty", false, fmaPut.isEmpty()); - assertEquals(label + ":put-instance-equals", fmaPut, FlexibleMapAccessor.getInstance(putText)); - assertNotEquals(label + ":put-not-equals-other", fmaPut, FlexibleMapAccessorTests.class); - - FlexibleStringExpander fse = FlexibleStringExpander.getInstance(fseText); - if (locale == null) { - assertNull(label + ":get-initial", fmaGet.get(testMap)); - fmaPut.put(testMap, var); - assertFalse(label + ":testMap-not-empty", testMap.isEmpty()); - assertEquals(label + ":get", var, fmaGet.get(testMap)); - assertEquals(label, value, fse.expandString(testMap)); - assertEquals(label + ":remove", var, fmaGet.remove(testMap)); - assertNull(label + ":remove-not-exist", fmaGet.remove(testMap)); - } else { - fmaPut.put(testMap, var); - assertFalse(label + ":testMap-not-empty", testMap.isEmpty()); - assertEquals(label + ":get", value, fmaGet.get(testMap, locale)); - // BUG: fmaGet modifies testMap, even tho it shouldn't - assertEquals(label + ":get", value, fmaGet.get(testMap, null)); - assertEquals(label, value, fse.expandString(testMap, locale)); - } - - testMap.clear(); - fmaPut.put(testMap, null); - assertFalse(label + ":testMap-not-empty-put-null", testMap.isEmpty()); - if (locale == null) { - assertNull(label + ":get-put-null", fmaGet.get(testMap)); - } - testMap.clear(); - Exception caught = null; - try { - fmaPut.put(null, var); - } catch (Exception e) { - caught = e; - } finally { - assertNotNull(label + ":put-null-map", caught); - assertTrue(label + ":put-null-map-isEmpty", testMap.isEmpty()); - } - Set<FlexibleMapAccessor<?>> set = new HashSet<FlexibleMapAccessor<?>>(); - assertFalse(label + ":not-in-set", set.contains(fmaGet)); - set.add(fmaGet); - assertTrue(label + ":in-set", set.contains(fmaGet)); - } - - private static void fmaEmptyTest(String label, String text) { - FlexibleMapAccessor<Class<?>> fma = FlexibleMapAccessor.getInstance(text); - assertTrue(label + ":isEmpty", fma.isEmpty()); - Map<String, Object> testMap = new HashMap<String, Object>(); - assertNull(label + ":get", fma.get(null)); - assertNull(label + ":get", fma.get(testMap)); - assertTrue(label + ":map-isEmpty-initial", testMap.isEmpty()); - fma.put(testMap, FlexibleMapAccessorTests.class); - assertTrue(label + ":map-isEmpty-map", testMap.isEmpty()); - fma.put(null, FlexibleMapAccessorTests.class); - assertTrue(label + ":map-isEmpty-null", testMap.isEmpty()); - assertSame(label + ":same-null", fmaNull, fma); - assertSame(label + ":same-empty", fmaEmpty, fma); - assertEquals(label + ":original-name", "", fma.getOriginalName()); - assertNull(label + ":remove", fma.remove(testMap)); - assertNotNull(label + ":toString", fma.toString()); - } - - // These tests rely upon FlexibleStringExpander, so they - // should follow the FlexibleStringExpander tests. - public void testFlexibleMapAccessor() { - fmaEmptyTest("fmaEmpty", ""); - fmaEmptyTest("fmaNull", null); - fmaEmptyTest("fma\"null\"", "null"); - fmaTest("UEL auto-vivify Map", "parameters.var", "Hello ${parameters.var}!", "World", "Hello World!"); - fmaTest("UEL auto-vivify List", "parameters.someList[0]", "parameters.someList[+0]", "Hello ${parameters.someList[0]}!", null, "World", "Hello World!"); - fmaTest("fse", "para${'meter'}s.var", "Hello ${parameters.var}!", "World", "Hello World!"); - fmaTest("foo", "'The total is ${total?currency(USD)}.'", "total", "The total is ${total?currency(USD)}.", localeToTest, new BigDecimal("12345678.90"), "The total is $12,345,678.90."); - assertTrue("containsNestedExpression method returns true", FlexibleMapAccessor.getInstance("Hello ${parameters.var}!").containsNestedExpression()); - assertFalse("containsNestedExpression method returns false", FlexibleMapAccessor.getInstance("Hello World!").containsNestedExpression()); - } - - public static class ThrowException { - public Object getValue() throws Exception { - throw new Exception(); - } - - public void setValue(Object value) throws Exception { - throw new Exception(); - } - } - - @SuppressWarnings("serial") - public static class CantRemoveMap<K, V> extends HashMap<K, V> { - @Override - public V get(Object key) { - return super.get(key); - } - - @Override - public V put(K key, V value) { - if (value == null) { - throw new IllegalArgumentException(); - } - return super.put(key, value); - } - } - - public void testVerbosityAndErrors() { - boolean isVerbose = Debug.isOn(Debug.VERBOSE); - try { - Debug.set(Debug.VERBOSE, true); - Map<String, Object> testMap = new CantRemoveMap<String, Object>(); - testMap.put("throwException", new ThrowException()); - assertNull("no var", FlexibleMapAccessor.getInstance("var").get(testMap)); - Object result = FlexibleMapAccessor.getInstance("throwException.value").get(testMap); - assertNull("get null var", result); - FlexibleMapAccessor.getInstance("throwException.value").put(testMap, this); - FlexibleMapAccessor.getInstance("throwException").remove(testMap); - assertNotNull("not removed", testMap.get("throwException")); - } finally { - Debug.set(Debug.VERBOSE, isVerbose); - } - } -} +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.base.util.collections.test; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import java.math.BigDecimal; + +import org.ofbiz.base.lang.SourceMonitored; +import org.ofbiz.base.test.GenericTestCaseBase; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.base.util.string.FlexibleStringExpander; + +@SourceMonitored +public class FlexibleMapAccessorTests extends GenericTestCaseBase { + private static final Locale localeToTest = new Locale("en", "US"); + private static FlexibleMapAccessor<?> fmaEmpty = FlexibleMapAccessor.getInstance(""); + private static FlexibleMapAccessor<?> fmaNull = FlexibleMapAccessor.getInstance(null); + + public FlexibleMapAccessorTests(String name) { + super(name); + } + + private static <T> void fmaTest(String label, String getText, String fseText, T var, String value) { + fmaTest(label, getText, getText, fseText, null, var, value); + } + + private static <T> void fmaTest(String label, String getText, String putText, String fseText, Locale locale, T var, String value) { + Map<String, Object> testMap = new HashMap<String, Object>(); + FlexibleMapAccessor<T> fmaGet = FlexibleMapAccessor.getInstance(getText); + assertEquals(label + ":get-original-name", getText, fmaGet.getOriginalName()); + assertEquals(label + ":get-isEmpty", false, fmaGet.isEmpty()); + assertEquals(label + ":get-instance-equals", fmaGet, FlexibleMapAccessor.getInstance(getText)); + assertEquals(label + ":toString", getText, fmaGet.toString()); + assertNotEquals(label + ":get-not-equals-empty", fmaEmpty, fmaGet); + assertNotEquals(label + ":get-not-equals-null", fmaNull, fmaGet); + assertNotEquals(label + ":empty-not-equals-get", fmaGet, fmaEmpty); + assertNotEquals(label + ":null-not-equals-get", fmaGet, fmaNull); + assertNotEquals(label + ":get-not-equals-other", fmaGet, FlexibleMapAccessorTests.class); + assertEquals(label + ":get-toString", getText, fmaGet.toString()); + FlexibleMapAccessor<T> fmaGetAscending = FlexibleMapAccessor.getInstance("+" + getText); + assertEquals(label + ":get-ascending-toString", "+" + getText, fmaGetAscending.toString()); + assertTrue(label + ":get-ascending-isAscending", fmaGetAscending.getIsAscending()); + FlexibleMapAccessor<T> fmaGetDescending = FlexibleMapAccessor.getInstance("-" + getText); + assertEquals(label + ":get-descending-toString", "-" + getText, fmaGetDescending.toString()); + assertFalse(label + ":get-decending-isAscending", fmaGetDescending.getIsAscending()); + FlexibleMapAccessor<T> fmaPut = FlexibleMapAccessor.getInstance(putText); + assertEquals(label + ":put-toString", putText, fmaPut.toString()); + assertEquals(label + ":put-original-name", putText, fmaPut.getOriginalName()); + assertEquals(label + ":put-isEmpty", false, fmaPut.isEmpty()); + assertEquals(label + ":put-instance-equals", fmaPut, FlexibleMapAccessor.getInstance(putText)); + assertNotEquals(label + ":put-not-equals-other", fmaPut, FlexibleMapAccessorTests.class); + + FlexibleStringExpander fse = FlexibleStringExpander.getInstance(fseText); + if (locale == null) { + assertNull(label + ":get-initial", fmaGet.get(testMap)); + fmaPut.put(testMap, var); + assertFalse(label + ":testMap-not-empty", testMap.isEmpty()); + assertEquals(label + ":get", var, fmaGet.get(testMap)); + assertEquals(label, value, fse.expandString(testMap)); + assertEquals(label + ":remove", var, fmaGet.remove(testMap)); + assertNull(label + ":remove-not-exist", fmaGet.remove(testMap)); + } else { + fmaPut.put(testMap, var); + assertFalse(label + ":testMap-not-empty", testMap.isEmpty()); + assertEquals(label + ":get", value, fmaGet.get(testMap, locale)); + // BUG: fmaGet modifies testMap, even tho it shouldn't + assertEquals(label + ":get", value, fmaGet.get(testMap, null)); + assertEquals(label, value, fse.expandString(testMap, locale)); + } + + testMap.clear(); + fmaPut.put(testMap, null); + assertFalse(label + ":testMap-not-empty-put-null", testMap.isEmpty()); + if (locale == null) { + assertNull(label + ":get-put-null", fmaGet.get(testMap)); + } + testMap.clear(); + Exception caught = null; + try { + fmaPut.put(null, var); + } catch (Exception e) { + caught = e; + } finally { + assertNotNull(label + ":put-null-map", caught); + assertTrue(label + ":put-null-map-isEmpty", testMap.isEmpty()); + } + Set<FlexibleMapAccessor<?>> set = new HashSet<FlexibleMapAccessor<?>>(); + assertFalse(label + ":not-in-set", set.contains(fmaGet)); + set.add(fmaGet); + assertTrue(label + ":in-set", set.contains(fmaGet)); + } + + private static void fmaEmptyTest(String label, String text) { + FlexibleMapAccessor<Class<?>> fma = FlexibleMapAccessor.getInstance(text); + assertTrue(label + ":isEmpty", fma.isEmpty()); + Map<String, Object> testMap = new HashMap<String, Object>(); + assertNull(label + ":get", fma.get(null)); + assertNull(label + ":get", fma.get(testMap)); + assertTrue(label + ":map-isEmpty-initial", testMap.isEmpty()); + fma.put(testMap, FlexibleMapAccessorTests.class); + assertTrue(label + ":map-isEmpty-map", testMap.isEmpty()); + fma.put(null, FlexibleMapAccessorTests.class); + assertTrue(label + ":map-isEmpty-null", testMap.isEmpty()); + assertSame(label + ":same-null", fmaNull, fma); + assertSame(label + ":same-empty", fmaEmpty, fma); + assertEquals(label + ":original-name", "", fma.getOriginalName()); + assertNull(label + ":remove", fma.remove(testMap)); + assertNotNull(label + ":toString", fma.toString()); + } + + // These tests rely upon FlexibleStringExpander, so they + // should follow the FlexibleStringExpander tests. + public void testFlexibleMapAccessor() { + fmaEmptyTest("fmaEmpty", ""); + fmaEmptyTest("fmaNull", null); + fmaEmptyTest("fma\"null\"", "null"); + fmaTest("UEL auto-vivify Map", "parameters.var", "Hello ${parameters.var}!", "World", "Hello World!"); + fmaTest("UEL auto-vivify List", "parameters.someList[0]", "parameters.someList[+0]", "Hello ${parameters.someList[0]}!", null, "World", "Hello World!"); + fmaTest("fse", "para${'meter'}s.var", "Hello ${parameters.var}!", "World", "Hello World!"); + fmaTest("foo", "'The total is ${total?currency(USD)}.'", "total", "The total is ${total?currency(USD)}.", localeToTest, new BigDecimal("12345678.90"), "The total is $12,345,678.90."); + assertTrue("containsNestedExpression method returns true", FlexibleMapAccessor.getInstance("Hello ${parameters.var}!").containsNestedExpression()); + assertFalse("containsNestedExpression method returns false", FlexibleMapAccessor.getInstance("Hello World!").containsNestedExpression()); + } + + public static class ThrowException { + public Object getValue() throws Exception { + throw new Exception(); + } + + public void setValue(Object value) throws Exception { + throw new Exception(); + } + } + + @SuppressWarnings("serial") + public static class CantRemoveMap<K, V> extends HashMap<K, V> { + @Override + public V get(Object key) { + return super.get(key); + } + + @Override + public V put(K key, V value) { + if (value == null) { + throw new IllegalArgumentException(); + } + return super.put(key, value); + } + } + + public void testVerbosityAndErrors() { + boolean isVerbose = Debug.isOn(Debug.VERBOSE); + try { + Debug.set(Debug.VERBOSE, true); + Map<String, Object> testMap = new CantRemoveMap<String, Object>(); + testMap.put("throwException", new ThrowException()); + assertNull("no var", FlexibleMapAccessor.getInstance("var").get(testMap)); + Object result = FlexibleMapAccessor.getInstance("throwException.value").get(testMap); + assertNull("get null var", result); + FlexibleMapAccessor.getInstance("throwException.value").put(testMap, this); + FlexibleMapAccessor.getInstance("throwException").remove(testMap); + assertNotNull("not removed", testMap.get("throwException")); + } finally { + Debug.set(Debug.VERBOSE, isVerbose); + } + } +} Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/test/FlexibleMapAccessorTests.java ('svn:eol-style' removed) Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/test/GenericMapTest.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/test/GenericMapTest.java?rev=1695126&r1=1695125&r2=1695126&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/test/GenericMapTest.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/test/GenericMapTest.java Mon Aug 10 16:15:37 2015 @@ -1,171 +1,171 @@ -/******************************************************************************* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - *******************************************************************************/ -package org.ofbiz.base.util.collections.test; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import org.ofbiz.base.test.GenericTestCaseBase; -import org.ofbiz.base.util.collections.GenericMap; -import org.ofbiz.base.util.collections.GenericMapEntry; -import org.ofbiz.base.util.collections.IteratorWrapper; - -public class GenericMapTest extends GenericTestCaseBase { - @SuppressWarnings("serial") - public static class TestGenericMap<K, V> extends GenericMap<K, V> { - private static final String[] countNames = { - "clearInternal", - "containsKey", - "get-true", - "get-false", - "isEmpty", - "iterator-true", - "iterator-false", - "putInternal", - "putAllIterator", - "removeInternal-true", - "removeInternal-false", - "size", - }; - protected final Map<String, Integer> counts = new HashMap<String, Integer>(); - protected final Map<K, V> proxyMap; - - protected TestGenericMap() { - this(null); - } - - protected TestGenericMap(Map<K, V> srcMap) { - for (String countName: countNames) { - counts.put(countName, 0); - } - if (srcMap != null) { - proxyMap = new HashMap<K, V>(srcMap); - } else { - proxyMap = new HashMap<K, V>(); - } - } - - private void incrementCallCount(String name) { - counts.put(name, counts.get(name) + 1); - } - - public List<Integer> getCounts() { - List<Integer> result = new ArrayList<Integer>(); - for (String countName: countNames) { - result.add(counts.get(countName)); - } - return result; - } - - @Override - protected void clearInternal() { - incrementCallCount("clearInternal"); - proxyMap.clear(); - } - - public boolean containsKey(Object key) { - incrementCallCount("containsKey"); - return proxyMap.containsKey(key); - } - - @Override - protected V get(Object key, boolean noteAccess) { - incrementCallCount("get-" + noteAccess); - return proxyMap.get(key); - } - - public boolean isEmpty() { - incrementCallCount("isEmpty"); - return proxyMap.isEmpty(); - } - - @Override - protected Iterator<Map.Entry<K, V>> iterator(final boolean noteAccess) { - incrementCallCount("iterator-" + noteAccess); - //return new IteratorWrapper<Map.Entry<K, V>, Map.Entry<K, V>>(noteAccess, proxyMap.entrySet().iterator()) { - return new IteratorWrapper<Map.Entry<K, V>, Map.Entry<K, V>>(proxyMap.entrySet().iterator()) { - @Override - protected Map.Entry<K, V> convert(Map.Entry<K, V> src) { - return new GenericMapEntry<K, V>(TestGenericMap.this, src.getKey(), noteAccess); - } - @Override - protected void noteRemoval(Map.Entry<K, V> dest, Map.Entry<K, V> src) { - } - }; - } - - public V put(K key, V value) { - incrementCallCount("putInternal"); - if (!proxyMap.containsKey(key)) incrementModCount(); - return proxyMap.put(key, value); - } - - @Override - protected <KE extends K, VE extends V> void putAllIterator(Iterator<Map.Entry<KE, VE>> it) { - incrementCallCount("putAllIterator"); - while (it.hasNext()) { - Map.Entry<KE, VE> entry = it.next(); - proxyMap.put(entry.getKey(), entry.getValue()); - } - } - - @Override - protected V removeInternal(Object key, boolean incrementModCount) { - incrementCallCount("removeInternal-" + incrementModCount); - if (!proxyMap.containsKey(key)) return null; - if (incrementModCount) incrementModCount(); - return proxyMap.remove(key); - } - - public int size() { - incrementCallCount("size"); - return proxyMap.size(); - } - } - - public GenericMapTest(String name) { - super(name); - } - - public void testFoo() throws Exception { - TestGenericMap<String, Integer> map = new TestGenericMap<String, Integer>(); - map.put("a", 0); System.err.println("put a\t\tcounts=" + map.getCounts() + ", modCount=" + map.getModCount()); - assertEquals("get a", Integer.valueOf(0), map.get("a")); - map.put("b", 1); System.err.println("put b\t\tcounts=" + map.getCounts() + ", modCount=" + map.getModCount()); - assertEquals("get b", Integer.valueOf(1), map.get("b")); - map.put("c", 2); System.err.println("put c\t\tcounts=" + map.getCounts() + ", modCount=" + map.getModCount()); - assertEquals("get c", Integer.valueOf(2), map.get("c")); - map.put("d", 3); System.err.println("put d\t\tcounts=" + map.getCounts() + ", modCount=" + map.getModCount()); - assertEquals("get d", Integer.valueOf(3), map.get("d")); - map.put("c", 22); System.err.println("put c-2\t\tcounts=" + map.getCounts() + ", modCount=" + map.getModCount()); - assertEquals("get c-2", Integer.valueOf(22), map.get("c")); - map.remove("b"); System.err.println("remove b\tcounts=" + map.getCounts() + ", modCount=" + map.getModCount()); - assertNull("null b", map.get("b")); - map.remove("aaa"); System.err.println("remove aaa\tcounts=" + map.getCounts() + ", modCount=" + map.getModCount()); - System.err.println("map=" + map); - System.err.println("counts=" + map.getCounts() + ", modCount=" + map.getModCount()); - // this seems to call size() - new HashMap<String, Integer>(map); - System.err.println("counts=" + map.getCounts() + ", modCount=" + map.getModCount()); - } - -} +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.base.util.collections.test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.ofbiz.base.test.GenericTestCaseBase; +import org.ofbiz.base.util.collections.GenericMap; +import org.ofbiz.base.util.collections.GenericMapEntry; +import org.ofbiz.base.util.collections.IteratorWrapper; + +public class GenericMapTest extends GenericTestCaseBase { + @SuppressWarnings("serial") + public static class TestGenericMap<K, V> extends GenericMap<K, V> { + private static final String[] countNames = { + "clearInternal", + "containsKey", + "get-true", + "get-false", + "isEmpty", + "iterator-true", + "iterator-false", + "putInternal", + "putAllIterator", + "removeInternal-true", + "removeInternal-false", + "size", + }; + protected final Map<String, Integer> counts = new HashMap<String, Integer>(); + protected final Map<K, V> proxyMap; + + protected TestGenericMap() { + this(null); + } + + protected TestGenericMap(Map<K, V> srcMap) { + for (String countName: countNames) { + counts.put(countName, 0); + } + if (srcMap != null) { + proxyMap = new HashMap<K, V>(srcMap); + } else { + proxyMap = new HashMap<K, V>(); + } + } + + private void incrementCallCount(String name) { + counts.put(name, counts.get(name) + 1); + } + + public List<Integer> getCounts() { + List<Integer> result = new ArrayList<Integer>(); + for (String countName: countNames) { + result.add(counts.get(countName)); + } + return result; + } + + @Override + protected void clearInternal() { + incrementCallCount("clearInternal"); + proxyMap.clear(); + } + + public boolean containsKey(Object key) { + incrementCallCount("containsKey"); + return proxyMap.containsKey(key); + } + + @Override + protected V get(Object key, boolean noteAccess) { + incrementCallCount("get-" + noteAccess); + return proxyMap.get(key); + } + + public boolean isEmpty() { + incrementCallCount("isEmpty"); + return proxyMap.isEmpty(); + } + + @Override + protected Iterator<Map.Entry<K, V>> iterator(final boolean noteAccess) { + incrementCallCount("iterator-" + noteAccess); + //return new IteratorWrapper<Map.Entry<K, V>, Map.Entry<K, V>>(noteAccess, proxyMap.entrySet().iterator()) { + return new IteratorWrapper<Map.Entry<K, V>, Map.Entry<K, V>>(proxyMap.entrySet().iterator()) { + @Override + protected Map.Entry<K, V> convert(Map.Entry<K, V> src) { + return new GenericMapEntry<K, V>(TestGenericMap.this, src.getKey(), noteAccess); + } + @Override + protected void noteRemoval(Map.Entry<K, V> dest, Map.Entry<K, V> src) { + } + }; + } + + public V put(K key, V value) { + incrementCallCount("putInternal"); + if (!proxyMap.containsKey(key)) incrementModCount(); + return proxyMap.put(key, value); + } + + @Override + protected <KE extends K, VE extends V> void putAllIterator(Iterator<Map.Entry<KE, VE>> it) { + incrementCallCount("putAllIterator"); + while (it.hasNext()) { + Map.Entry<KE, VE> entry = it.next(); + proxyMap.put(entry.getKey(), entry.getValue()); + } + } + + @Override + protected V removeInternal(Object key, boolean incrementModCount) { + incrementCallCount("removeInternal-" + incrementModCount); + if (!proxyMap.containsKey(key)) return null; + if (incrementModCount) incrementModCount(); + return proxyMap.remove(key); + } + + public int size() { + incrementCallCount("size"); + return proxyMap.size(); + } + } + + public GenericMapTest(String name) { + super(name); + } + + public void testFoo() throws Exception { + TestGenericMap<String, Integer> map = new TestGenericMap<String, Integer>(); + map.put("a", 0); System.err.println("put a\t\tcounts=" + map.getCounts() + ", modCount=" + map.getModCount()); + assertEquals("get a", Integer.valueOf(0), map.get("a")); + map.put("b", 1); System.err.println("put b\t\tcounts=" + map.getCounts() + ", modCount=" + map.getModCount()); + assertEquals("get b", Integer.valueOf(1), map.get("b")); + map.put("c", 2); System.err.println("put c\t\tcounts=" + map.getCounts() + ", modCount=" + map.getModCount()); + assertEquals("get c", Integer.valueOf(2), map.get("c")); + map.put("d", 3); System.err.println("put d\t\tcounts=" + map.getCounts() + ", modCount=" + map.getModCount()); + assertEquals("get d", Integer.valueOf(3), map.get("d")); + map.put("c", 22); System.err.println("put c-2\t\tcounts=" + map.getCounts() + ", modCount=" + map.getModCount()); + assertEquals("get c-2", Integer.valueOf(22), map.get("c")); + map.remove("b"); System.err.println("remove b\tcounts=" + map.getCounts() + ", modCount=" + map.getModCount()); + assertNull("null b", map.get("b")); + map.remove("aaa"); System.err.println("remove aaa\tcounts=" + map.getCounts() + ", modCount=" + map.getModCount()); + System.err.println("map=" + map); + System.err.println("counts=" + map.getCounts() + ", modCount=" + map.getModCount()); + // this seems to call size() + new HashMap<String, Integer>(map); + System.err.println("counts=" + map.getCounts() + ", modCount=" + map.getModCount()); + } + +} Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/test/GenericMapTest.java ('svn:eol-style' removed) |
Free forum by Nabble | Edit this page |