Skip to content

Commit

Permalink
[Java] Fix warnings and improve code after merge of PR real-logic#202.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpt777 committed Feb 6, 2020
1 parent 13a9083 commit b35db2a
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 300 deletions.
105 changes: 56 additions & 49 deletions agrona/src/main/java/org/agrona/collections/Int2IntHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -861,49 +861,7 @@ public Entry<Integer, Integer> next()

private Entry<Integer, Integer> allocateDuplicateEntry()
{
final int k = getIntKey();
final int v = getIntValue();

return new Entry<Integer, Integer>()
{
public Integer getKey()
{
return k;
}

public Integer getValue()
{
return v;
}

public Integer setValue(final Integer value)
{
return Int2IntHashMap.this.put(k, value.intValue());
}

@DoNotSub public int hashCode()
{
return Integer.hashCode(getIntKey()) ^ Integer.hashCode(getIntValue());
}

@DoNotSub public boolean equals(final Object o)
{
if (!(o instanceof Entry))
{
return false;
}

final Map.Entry<?, ?> e = (Entry<?, ?>)o;

return (e.getKey() != null && e.getValue() != null) &&
(e.getKey().equals(k) && e.getValue().equals(v));
}

public String toString()
{
return k + "=" + v;
}
};
return new MapEntry(getIntKey(), getIntValue());
}

/**
Expand Down Expand Up @@ -933,6 +891,55 @@ public boolean equals(final Object o)

return Objects.equals(getKey(), that.getKey()) && Objects.equals(getValue(), that.getValue());
}

public final class MapEntry implements Entry<Integer, Integer>
{
private final int k;
private final int v;

public MapEntry(final int k, final int v)
{
this.k = k;
this.v = v;
}

public Integer getKey()
{
return k;
}

public Integer getValue()
{
return v;
}

public Integer setValue(final Integer value)
{
return Int2IntHashMap.this.put(k, value.intValue());
}

@DoNotSub public int hashCode()
{
return Integer.hashCode(getIntKey()) ^ Integer.hashCode(getIntValue());
}

@DoNotSub public boolean equals(final Object o)
{
if (!(o instanceof Map.Entry))
{
return false;
}

final Entry<?, ?> e = (Entry<?, ?>)o;

return (e.getKey() != null && e.getValue() != null) && (e.getKey().equals(k) && e.getValue().equals(v));
}

public String toString()
{
return k + "=" + v;
}
}
}

/**
Expand Down Expand Up @@ -1106,22 +1113,21 @@ public boolean contains(final Object o)
/**
* {@inheritDoc}
*/
@Override
public Object[] toArray()
{
final Object[] array = new Object[size()];
return toArray(array);
return toArray(new Object[size()]);
}

/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public <T> T[] toArray(final T[] a)
{
final T[] array = a.length >= size ? a : (T[])java.lang.reflect.Array
.newInstance(a.getClass().getComponentType(), size);
final T[] array = a.length >= size ?
a : (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
final EntryIterator it = iterator();

for (@DoNotSub int i = 0; i < array.length; i++)
{
if (it.hasNext())
Expand All @@ -1135,6 +1141,7 @@ public <T> T[] toArray(final T[] a)
break;
}
}

return array;
}
}
Expand Down
106 changes: 57 additions & 49 deletions agrona/src/main/java/org/agrona/collections/Int2ObjectHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -778,22 +778,21 @@ public boolean contains(final Object o)
/**
* {@inheritDoc}
*/
@Override
public Object[] toArray()
{
final Object[] array = new Object[size()];
return toArray(array);
return toArray(new Object[size()]);
}

/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public <T> T[] toArray(final T[] a)
{
final T[] array = a.length >= size ? a : (T[])java.lang.reflect.Array
.newInstance(a.getClass().getComponentType(), size);
final T[] array = a.length >= size ?
a : (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
final EntryIterator it = iterator();

for (@DoNotSub int i = 0; i < array.length; i++)
{
if (it.hasNext())
Expand All @@ -807,6 +806,7 @@ public <T> T[] toArray(final T[] a)
break;
}
}

return array;
}
}
Expand Down Expand Up @@ -958,49 +958,7 @@ public Entry<Integer, V> next()

private Entry<Integer, V> allocateDuplicateEntry()
{
final int k = getIntKey();
final V v = getValue();

return new Entry<Integer, V>()
{
public Integer getKey()
{
return k;
}

public V getValue()
{
return v;
}

public V setValue(final V value)
{
return Int2ObjectHashMap.this.put(k, value);
}

@DoNotSub public int hashCode()
{
return Integer.hashCode(getIntKey()) ^ (v != null ? v.hashCode() : 0);
}

public boolean equals(final Object o)
{
if (!(o instanceof Entry))
{
return false;
}

final Map.Entry<?, ?> e = (Entry<?, ?>)o;

return (e.getKey() != null && e.getKey().equals(k)) &&
((e.getValue() == null && v == null) || e.getValue().equals(v));
}

public String toString()
{
return k + "=" + v;
}
};
return new MapEntry(getIntKey(), getValue());
}

public Integer getKey()
Expand Down Expand Up @@ -1035,5 +993,55 @@ public V setValue(final V value)

return (V)oldValue;
}

public final class MapEntry implements Entry<Integer, V>
{
private final int k;
private final V v;

public MapEntry(final int k, final V v)
{
this.k = k;
this.v = v;
}

public Integer getKey()
{
return k;
}

public V getValue()
{
return v;
}

public V setValue(final V value)
{
return Int2ObjectHashMap.this.put(k, value);
}

@DoNotSub public int hashCode()
{
return Integer.hashCode(getIntKey()) ^ (v != null ? v.hashCode() : 0);
}

public boolean equals(final Object o)
{
if (!(o instanceof Map.Entry))
{
return false;
}

final Entry<?, ?> e = (Entry<?, ?>)o;

return (e.getKey() != null && e.getKey().equals(k)) &&
((e.getValue() == null && v == null) || e.getValue().equals(v));
}

public String toString()
{
return k + "=" + v;
}
}
}
}
Loading

0 comments on commit b35db2a

Please sign in to comment.