Skip to content

Commit

Permalink
Suppressed unchecked warnings. Small fix to SimpleDateFormatter - res…
Browse files Browse the repository at this point in the history
…et timezone and number formatter when grabbing instance via constructor.
  • Loading branch information
jdereg committed Oct 7, 2023
1 parent a41dbe5 commit 2fd4afd
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 44 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/cedarsoftware/util/ArrayUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public static <T> T[] shallowCopy(final T[] array)
* @return The new array, <code>null</code> if <code>null</code> array inputs.
* The type of the new array is the type of the first array.
*/
@SuppressWarnings("unchecked")
public static <T> T[] addAll(final T[] array1, final T[] array2)
{
if (array1 == null)
Expand All @@ -134,6 +135,7 @@ else if (array2 == null)
return newArray;
}

@SuppressWarnings("unchecked")
public static <T> T[] removeItem(T[] array, int pos)
{
final int len = Array.getLength(array);
Expand All @@ -156,9 +158,10 @@ public static <T> T[] getArraySubset(T[] array, int start, int end)
* @param <T> Type of the array
* @return Array of the type (T) containing the items from collection 'c'.
*/
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Class<T> classToCastTo, Collection<?> c)
{
T[] array = (T[]) c.toArray((T[]) Array.newInstance(classToCastTo, c.size()));
T[] array = c.toArray((T[]) Array.newInstance(classToCastTo, c.size()));
Iterator i = c.iterator();
int idx = 0;
while (i.hasNext())
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/cedarsoftware/util/CaseInsensitiveMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ else if (m instanceof HashMap)
}
}

@SuppressWarnings("unchecked")
protected Map<K, V> copy(Map<K, V> source, Map<K, V> dest)
{
for (Entry<K, V> entry : source.entrySet())
Expand Down Expand Up @@ -175,6 +176,7 @@ public boolean containsKey(Object key)
return map.containsKey(key);
}

@SuppressWarnings("unchecked")
public V put(K key, V value)
{
if (key instanceof String)
Expand All @@ -185,6 +187,7 @@ public V put(K key, V value)
return map.put(key, value);
}

@SuppressWarnings("unchecked")
public Object putObject(Object key, Object value)
{ // not calling put() to save a little speed.
if (key instanceof String)
Expand All @@ -195,6 +198,7 @@ public Object putObject(Object key, Object value)
return map.put((K)key, (V)value);
}

@SuppressWarnings("unchecked")
public void putAll(Map<? extends K, ? extends V> m)
{
if (MapUtilities.isEmpty(m))
Expand Down Expand Up @@ -353,7 +357,8 @@ public boolean removeAll(Collection c)
return map.size() != size;
}

public boolean retainAll(Collection c)
@SuppressWarnings("unchecked")
public boolean retainAll(Collection<?> c)
{
Map<K, V> other = new CaseInsensitiveMap<>();
for (Object o : c)
Expand Down Expand Up @@ -406,6 +411,7 @@ public int hashCode()
return h;
}

@SuppressWarnings("unchecked")
public Iterator<K> iterator()
{
iter = map.keySet().iterator();
Expand Down Expand Up @@ -437,6 +443,7 @@ public Set<Entry<K, V>> entrySet()
public boolean isEmpty() { return map.isEmpty(); }
public void clear() { map.clear(); }

@SuppressWarnings("unchecked")
public boolean contains(Object o)
{
if (!(o instanceof Entry))
Expand All @@ -453,6 +460,7 @@ public boolean contains(Object o)
return false;
}

@SuppressWarnings("unchecked")
public boolean remove(Object o)
{
if (!(o instanceof Entry))
Expand All @@ -470,6 +478,7 @@ public boolean remove(Object o)
* on iterator solution. This method is fast because contains()
* and remove() are both hashed O(1) look ups.
*/
@SuppressWarnings("unchecked")
public boolean removeAll(Collection c)
{
final int size = map.size();
Expand All @@ -484,6 +493,7 @@ public boolean removeAll(Collection c)
return map.size() != size;
}

@SuppressWarnings("unchecked")
public boolean retainAll(Collection c)
{
// Create fast-access O(1) to all elements within passed in Collection
Expand Down Expand Up @@ -549,6 +559,7 @@ public CaseInsensitiveEntry(Entry<K, V> entry)
super(entry);
}

@SuppressWarnings("unchecked")
public K getKey()
{
K superKey = super.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@SuppressWarnings("unchecked")
public class CaseInsensitiveSet<E> implements Set<E>
{
private final Map<E, Object> map;
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/com/cedarsoftware/util/CompactMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@SuppressWarnings("unchecked")
public class CompactMap<K, V> implements Map<K, V>
{
private static final String EMPTY_MAP = "_︿_ψ_☼";
Expand All @@ -90,7 +91,7 @@ public CompactMap(Map<K, V> other)
this();
putAll(other);
}

public int size()
{
if (val instanceof Object[])
Expand Down Expand Up @@ -566,7 +567,7 @@ public boolean removeAll(Collection c)
}
return size() != size;
}

public boolean retainAll(Collection c)
{
// Create fast-access O(1) to all elements within passed in Collection
Expand Down Expand Up @@ -620,7 +621,7 @@ public Iterator<V> iterator()

public Set<Entry<K, V>> entrySet()
{
return new AbstractSet<Entry<K,V>>()
return new AbstractSet<>()
{
public Iterator<Entry<K, V>> iterator()
{
Expand Down Expand Up @@ -922,10 +923,11 @@ protected Map<K, V> getNewMap(int size)
}
protected boolean isCaseInsensitive() { return false; }
protected int compactSize() { return 80; }

protected boolean useCopyIterator() {
Map newMap = getNewMap();
Map<K, V> newMap = getNewMap();
if (newMap instanceof CaseInsensitiveMap) {
newMap = ((CaseInsensitiveMap) newMap).getWrappedMap();
newMap = ((CaseInsensitiveMap<K, V>) newMap).getWrappedMap();
}
return newMap instanceof SortedMap;
}
Expand All @@ -944,7 +946,7 @@ abstract class CompactIterator {
current = EMPTY_MAP;
index = -1;
if (val instanceof Map) {
mapIterator = ((Map)val).entrySet().iterator();
mapIterator = ((Map<K, V>)val).entrySet().iterator();
}
}

Expand Down Expand Up @@ -1073,16 +1075,16 @@ public final void remove() {

final class CopyKeyIterator extends CopyIterator
implements Iterator<K> {
public final K next() { return nextEntry().getKey(); }
public K next() { return nextEntry().getKey(); }
}

final class CopyValueIterator extends CopyIterator
implements Iterator<V> {
public final V next() { return nextEntry().getValue(); }
public V next() { return nextEntry().getValue(); }
}

final class CopyEntryIterator extends CompactMap.CopyIterator
implements Iterator<Map.Entry<K,V>> {
public final Map.Entry<K,V> next() { return nextEntry(); }
public Map.Entry<K,V> next() { return nextEntry(); }
}
}
11 changes: 9 additions & 2 deletions src/main/java/com/cedarsoftware/util/CompactSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private boolean compareItems(Object item, Object anItem)
return Objects.equals(item, anItem);
}

@SuppressWarnings("unchecked")
public boolean contains(Object item)
{
if (val instanceof Object[])
Expand All @@ -127,11 +128,12 @@ else if (val instanceof Set)
return false;
}

@SuppressWarnings("unchecked")
public Iterator<E> iterator()
{
return new Iterator<E>()
{
Iterator<E> iter = getCopy().iterator();
final Iterator<E> iter = getCopy().iterator();
E currentEntry = (E) NO_ENTRY;

public boolean hasNext() { return iter.hasNext(); }
Expand All @@ -154,6 +156,7 @@ public void remove()
};
}

@SuppressWarnings("unchecked")
private Set<E> getCopy()
{
Set<E> copy = getNewSet(size()); // Use their Set (TreeSet, HashSet, LinkedHashSet, etc.)
Expand All @@ -174,7 +177,8 @@ else if (val instanceof Set)
// }
return copy;
}


@SuppressWarnings("unchecked")
public boolean add(E item)
{
if (val instanceof Object[])
Expand Down Expand Up @@ -217,6 +221,7 @@ else if (val instanceof Set)
return true;
}

@SuppressWarnings("unchecked")
public boolean remove(Object item)
{
if (val instanceof Object[])
Expand Down Expand Up @@ -280,6 +285,8 @@ public void clear()
* @return new empty Set instance to use when size() becomes {@literal >} compactSize().
*/
protected Set<E> getNewSet() { return new HashSet<>(compactSize() + 1); }

@SuppressWarnings("unchecked")
protected Set<E> getNewSet(int size)
{
Set<E> set = getNewSet();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/cedarsoftware/util/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.text.MessageFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
Expand Down Expand Up @@ -168,6 +167,7 @@ private Converter() { }
* wrapper.
* @return An instanceof targetType class, based upon the value passed in.
*/
@SuppressWarnings("unchecked")
public static <T> T convert(Object fromInstance, Class<T> toType)
{
if (toType == null)
Expand Down Expand Up @@ -204,6 +204,7 @@ public static String convert2String(Object fromInstance)
* Calendar (returns ISO-DATE format: 2020-04-10T12:15:47), any Enum (returns Enum's name()), BigDecimal,
* BigInteger, AtomicBoolean, AtomicInteger, AtomicLong, and Character.
*/
@SuppressWarnings("unchecked")
public static String convertToString(Object fromInstance)
{
if (fromInstance == null)
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/cedarsoftware/util/DeepEquals.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@SuppressWarnings("unchecked")
public class DeepEquals
{
private DeepEquals () {}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/cedarsoftware/util/GraphComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@SuppressWarnings("unchecked")
public class GraphComparator
{
public static final String ROOT = "-root-";
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/cedarsoftware/util/ProxyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static <T> T create(Class<T> intf, InvocationHandler h) {
* if the invocation handler, <code>h</code>, is
* <code>null</code>
*/
@SuppressWarnings("unchecked")
public static <T> T create(ClassLoader loader, Class<T> intf, InvocationHandler h) {
return (T)Proxy.newProxyInstance(loader, new Class[]{intf}, h);
}
Expand Down
28 changes: 10 additions & 18 deletions src/main/java/com/cedarsoftware/util/SafeSimpleDateFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* This class implements a Thread-Safe (re-entrant) SimpleDateFormat
Expand All @@ -33,16 +32,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class SafeSimpleDateFormat extends Format
public class SafeSimpleDateFormat extends DateFormat
{
private final String _format;
private static final ThreadLocal<Map<String, SimpleDateFormat>> _dateFormats = new ThreadLocal<Map<String, SimpleDateFormat>>()
{
public Map<String, SimpleDateFormat> initialValue()
{
return new ConcurrentHashMap<>();
}
};
private static final ThreadLocal<Map<String, SimpleDateFormat>> _dateFormats = ThreadLocal.withInitial(ConcurrentHashMap::new);

public static SimpleDateFormat getDateFormat(String format)
{
Expand All @@ -63,23 +56,22 @@ public static SimpleDateFormat getDateFormat(String format)
public SafeSimpleDateFormat(String format)
{
_format = format;
DateFormat dateFormat = getDateFormat(_format);
// Reset for new instance
dateFormat.setNumberFormat(NumberFormat.getNumberInstance());
dateFormat.setTimeZone(TimeZone.getDefault());
}

public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition)
{
return getDateFormat(_format).format(obj, toAppendTo, pos);
return getDateFormat(_format).format(date, toAppendTo, fieldPosition);
}

public Object parseObject(String source, ParsePosition pos)
public Date parse(String source, ParsePosition pos)
{
return getDateFormat(_format).parse(source, pos);
}

public Date parse(String day) throws ParseException
{
return getDateFormat(_format).parse(day);
}

public void setTimeZone(TimeZone tz)
{
getDateFormat(_format).setTimeZone(tz);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/cedarsoftware/util/TrackingMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public TrackingMap(Map<K, V> map) {
readKeys = new HashSet<>();
}

@SuppressWarnings("unchecked")
public V get(Object key) {
V value = internalMap.get(key);
readKeys.add((K) key);
Expand All @@ -52,6 +53,7 @@ public V put(K key, V value)
return internalMap.put(key, value);
}

@SuppressWarnings("unchecked")
public boolean containsKey(Object key) {
boolean containsKey = internalMap.containsKey(key);
readKeys.add((K)key);
Expand Down Expand Up @@ -144,5 +146,5 @@ public void informAdditionalUsage(TrackingMap<K, V> additional) {
* Fetch the Map that this TrackingMap wraps.
* @return Map the wrapped Map
*/
public Map getWrappedMap() { return internalMap; }
public Map<K, V> getWrappedMap() { return internalMap; }
}
1 change: 1 addition & 0 deletions src/main/java/com/cedarsoftware/util/Traverser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@SuppressWarnings("unchecked")
public class Traverser
{
public interface Visitor
Expand Down
Loading

0 comments on commit 2fd4afd

Please sign in to comment.