Skip to content

Commit

Permalink
Reduce number of warnings in source code, utilized lambda's.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdereg committed Oct 13, 2023
1 parent f9e8a15 commit 2c4f891
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 70 deletions.
56 changes: 22 additions & 34 deletions src/main/java/com/cedarsoftware/util/CompactMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ else if (val == EMPTY_MAP)
// size == 1
if (compareKeys(key, getLogicalSingleKey()))
{ // Overwrite
Object save = getLogicalSingleValue();
V save = getLogicalSingleValue();
if (compareKeys(key, getSingleValueKey()) && !(value instanceof Map || value instanceof Object[]))
{
val = value;
Expand All @@ -300,7 +300,7 @@ else if (val == EMPTY_MAP)
{
val = new CompactMapEntry(key, value);
}
return (V) save;
return save;
}
else
{ // CompactMapEntry to []
Expand All @@ -318,9 +318,9 @@ public V remove(Object key)
{
if (val instanceof Object[])
{ // 2 to compactSize
Object[] entries = (Object[]) val;
if (size() == 2)
{ // When at 2 entries, we must drop back to CompactMapEntry or val (use clear() and put() to get us there).
Object[] entries = (Object[]) val;
if (compareKeys(key, entries[0]))
{
Object prevValue = entries[1];
Expand All @@ -335,12 +335,9 @@ else if (compareKeys(key, entries[2]))
put((K)entries[0], (V)entries[1]);
return (V) prevValue;
}

return null; // not found
}
else
{
Object[] entries = (Object[]) val;
final int len = entries.length;
for (int i = 0; i < len; i += 2)
{
Expand All @@ -355,9 +352,8 @@ else if (compareKeys(key, entries[2]))
return (V) prior;
}
}

return null; // not found
}
return null; // not found
}
else if (val instanceof Map)
{ // > compactSize
Expand Down Expand Up @@ -392,34 +388,34 @@ else if (val == EMPTY_MAP)
// size == 1
if (compareKeys(key, getLogicalSingleKey()))
{ // found
Object save = getLogicalSingleValue();
V save = getLogicalSingleValue();
val = EMPTY_MAP;
return (V) save;
return save;
}
else
{ // not found
return null;
}
}

public void putAll(Map<? extends K, ? extends V> m)
public void putAll(Map<? extends K, ? extends V> map)
{
if (m == null)
if (map == null)
{
return;
}
int mSize = m.size();
int mSize = map.size();
if (val instanceof Map || mSize > compactSize())
{
if (val == EMPTY_MAP)
{
val = getNewMap(mSize);
}
((Map) val).putAll(m);
((Map<K, V>) val).putAll(map);
}
else
{
for (Entry<? extends K, ? extends V> entry : m.entrySet())
for (Entry<? extends K, ? extends V> entry : map.entrySet())
{
put(entry.getKey(), entry.getValue());
}
Expand Down Expand Up @@ -583,15 +579,7 @@ public boolean retainAll(Collection c)
}

final int size = size();
Iterator<K> i = keySet().iterator();
while (i.hasNext())
{
K key = i.next();
if (!other.containsKey(key))
{
i.remove();
}
}
keySet().removeIf(key -> !other.containsKey(key));

return size() != size;
}
Expand Down Expand Up @@ -763,12 +751,12 @@ private void iteratorRemove(Entry<K, V> currentEntry, Iterator<Entry<K, V>> i)
remove(currentEntry.getKey());
}

public Map minus(Object removeMe)
public Map<K, V> minus(Object removeMe)
{
throw new UnsupportedOperationException("Unsupported operation [minus] or [-] between Maps. Use removeAll() or retainAll() instead.");
}

public Map plus(Object right)
public Map<K, V> plus(Object right)
{
throw new UnsupportedOperationException("Unsupported operation [plus] or [+] between Maps. Use putAll() instead.");
}
Expand Down Expand Up @@ -1004,8 +992,8 @@ public final void remove() {
}
}

final class CompactKeyIterator extends CompactMap.CompactIterator
implements Iterator<K> {
final class CompactKeyIterator extends CompactMap<K, V>.CompactIterator implements Iterator<K>
{
public final K next() {
advance();
if (mapIterator!=null) {
Expand All @@ -1016,8 +1004,8 @@ public final K next() {
}
}

final class CompactValueIterator extends CompactMap.CompactIterator
implements Iterator<V> {
final class CompactValueIterator extends CompactMap<K, V>.CompactIterator implements Iterator<V>
{
public final V next() {
advance();
if (mapIterator != null) {
Expand All @@ -1030,8 +1018,8 @@ public final V next() {
}
}

final class CompactEntryIterator extends CompactMap.CompactIterator
implements Iterator<Map.Entry<K,V>> {
final class CompactEntryIterator extends CompactMap<K, V>.CompactIterator implements Iterator<Map.Entry<K,V>>
{
public final Map.Entry<K,V> next() {
advance();
if (mapIterator != null) {
Expand Down Expand Up @@ -1083,8 +1071,8 @@ final class CopyValueIterator extends CopyIterator
public V next() { return nextEntry().getValue(); }
}

final class CopyEntryIterator extends CompactMap.CopyIterator
implements Iterator<Map.Entry<K,V>> {
final class CopyEntryIterator extends CompactMap<K, V>.CopyIterator implements Iterator<Map.Entry<K,V>>
{
public Map.Entry<K,V> next() { return nextEntry(); }
}
}
64 changes: 28 additions & 36 deletions src/main/java/com/cedarsoftware/util/GraphComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public enum Command
LIST_RESIZE("list.resize"),
LIST_SET_ELEMENT("list.setElement");

private String name;
private final String name;
Command(final String name)
{
this.name = name.intern();
Expand Down Expand Up @@ -487,7 +487,7 @@ public static List<Delta> compare(Object source, Object target, final ID idFetch
}

// source objects by ID
final Set potentialOrphans = new HashSet();
final Set<Object> potentialOrphans = new HashSet<>();
Traverser.traverse(source, new Traverser.Visitor()
{
public void process(Object o)
Expand All @@ -501,14 +501,10 @@ public void process(Object o)

// Remove all target objects from potential orphan map, leaving remaining objects
// that are no longer referenced in the potentialOrphans map.
Traverser.traverse(target, new Traverser.Visitor()
{
public void process(Object o)
Traverser.traverse(target, o -> {
if (isIdObject(o, idFetcher))
{
if (isIdObject(o, idFetcher))
{
potentialOrphans.remove(idFetcher.getId(o));
}
potentialOrphans.remove(idFetcher.getId(o));
}
});

Expand Down Expand Up @@ -585,7 +581,7 @@ private static void compareArrays(Delta delta, Collection<Delta> deltas, LinkedL
}

final String sysId = "(" + System.identityHashCode(delta.srcValue) + ')';
final Class compType = delta.targetValue.getClass().getComponentType();
final Class<?> compType = delta.targetValue.getClass().getComponentType();

if (isLogicalPrimitive(compType))
{
Expand Down Expand Up @@ -666,15 +662,16 @@ private static void copyArrayElement(Delta delta, Collection<Delta> deltas, Stri

/**
* Deeply compare two Sets and generate the appropriate 'add' or 'remove' commands
* to rectify their differences.
* to rectify their differences. Order of Sets does not matter (two equal Sets do
* not have to be in the same order).
*/
private static void compareSets(Delta delta, Collection<Delta> deltas, LinkedList<Delta> stack, ID idFetcher)
{
Set srcSet = (Set) delta.srcValue;
Set targetSet = (Set) delta.targetValue;
Set<?> srcSet = (Set<?>) delta.srcValue;
Set<?> targetSet = (Set<?>) delta.targetValue;

// Create ID to Object map for target Set
Map targetIdToValue = new HashMap();
Map<Object, Object> targetIdToValue = new HashMap<>();
for (Object targetValue : targetSet)
{
if (isIdObject(targetValue, idFetcher))
Expand All @@ -683,7 +680,7 @@ private static void compareSets(Delta delta, Collection<Delta> deltas, LinkedLis
}
}

Map srcIdToValue = new HashMap();
Map<Object, Object> srcIdToValue = new HashMap<>();
String sysId = "(" + System.identityHashCode(srcSet) + ").remove(";
for (Object srcValue : srcSet)
{
Expand Down Expand Up @@ -739,13 +736,13 @@ private static void compareSets(Delta delta, Collection<Delta> deltas, LinkedLis
}
}
}

// TODO: If LinkedHashSet, may need to issue commands to reorder...
}

/**
* Deeply compare two Maps and generate the appropriate 'put' or 'remove' commands
* to rectify their differences.
* to rectify their differences. Order of Maps des not matter from an equality standpoint.
* So for example, a TreeMap and a HashMap are considered equal (no Deltas) if they contain
* the same entries, regardless of order.
*/
private static void compareMaps(Delta delta, Collection<Delta> deltas, LinkedList<Delta> stack, ID idFetcher)
{
Expand All @@ -756,7 +753,7 @@ private static void compareMaps(Delta delta, Collection<Delta> deltas, LinkedLis
// If the key exists in both, then the value must tested for equivalence. If !equal, then a PUT command
// is created to re-associate target value to key.
final String sysId = "(" + System.identityHashCode(srcMap) + ')';
for (Map.Entry entry : srcMap.entrySet())
for (Map.Entry<Object, Object> entry : srcMap.entrySet())
{
Object srcKey = entry.getKey();
Object srcValue = entry.getValue();
Expand Down Expand Up @@ -796,7 +793,7 @@ else if (!DeepEquals.deepEquals(srcValue, targetValue))
}
}

for (Map.Entry entry : targetMap.entrySet())
for (Map.Entry<Object, Object> entry : targetMap.entrySet())
{
Object targetKey = entry.getKey();
String srcPtr = sysId + "['" + System.identityHashCode(targetKey) + "']";
Expand All @@ -808,7 +805,6 @@ else if (!DeepEquals.deepEquals(srcValue, targetValue))
deltas.add(putDelta);
}
}
// TODO: If LinkedHashMap, may need to issue commands to reorder...
}

private static void addMapPutDelta(Delta delta, Collection<Delta> deltas, String srcPtr, Object srcValue, Object targetValue, Object key)
Expand All @@ -824,8 +820,8 @@ private static void addMapPutDelta(Delta delta, Collection<Delta> deltas, String
*/
private static void compareLists(Delta delta, Collection<Delta> deltas, LinkedList<Delta> stack, ID idFetcher)
{
List srcList = (List) delta.srcValue;
List targetList = (List) delta.targetValue;
List<Object> srcList = (List<Object>) delta.srcValue;
List<Object> targetList = (List<Object>) delta.targetValue;
int srcLen = srcList.size();
int targetLen = targetList.size();

Expand Down Expand Up @@ -907,15 +903,11 @@ private static void copyListElement(Delta delta, Collection<Delta> deltas, Strin
public static List<DeltaError> applyDelta(Object source, List<Delta> commands, final ID idFetcher, DeltaProcessor deltaProcessor, boolean ... failFast)
{
// Index all objects in source graph
final Map srcMap = new HashMap();
Traverser.traverse(source, new Traverser.Visitor()
{
public void process(Object o)
final Map<Object, Object> srcMap = new HashMap<>();
Traverser.traverse(source, o -> {
if (isIdObject(o, idFetcher))
{
if (isIdObject(o, idFetcher))
{
srcMap.put(idFetcher.getId(o), o);
}
srcMap.put(idFetcher.getId(o), o);
}
});

Expand Down Expand Up @@ -1106,25 +1098,25 @@ public void processObjectTypeChanged(Object srcValue, Field field, Delta delta)

public void processSetAdd(Object source, Field field, Delta delta)
{
Set set = (Set) Helper.getFieldValueAs(source, field, Set.class, delta);
Set<Object> set = (Set<Object>) Helper.getFieldValueAs(source, field, Set.class, delta);
set.add(delta.getTargetValue());
}

public void processSetRemove(Object source, Field field, Delta delta)
{
Set set = (Set) Helper.getFieldValueAs(source, field, Set.class, delta);
Set<Object> set = (Set<Object>) Helper.getFieldValueAs(source, field, Set.class, delta);
set.remove(delta.getSourceValue());
}

public void processMapPut(Object source, Field field, Delta delta)
{
Map map = (Map) Helper.getFieldValueAs(source, field, Map.class, delta);
Map<Object, Object> map = (Map<Object, Object>) Helper.getFieldValueAs(source, field, Map.class, delta);
map.put(delta.optionalKey, delta.getTargetValue());
}

public void processMapRemove(Object source, Field field, Delta delta)
{
Map map = (Map) Helper.getFieldValueAs(source, field, Map.class, delta);
Map<Object, Object> map = (Map<Object, Object>) Helper.getFieldValueAs(source, field, Map.class, delta);
map.remove(delta.optionalKey);
}

Expand Down Expand Up @@ -1153,7 +1145,7 @@ else if (deltaLen < 0)

public void processListSetElement(Object source, Field field, Delta delta)
{
List list = (List) Helper.getFieldValueAs(source, field, List.class, delta);
List<Object> list = (List<Object>) Helper.getFieldValueAs(source, field, List.class, delta);
int pos = Helper.getResizeValue(delta);
int listLen = list.size();

Expand Down

0 comments on commit 2c4f891

Please sign in to comment.