diff options
Diffstat (limited to 'src/org/unitConverter/math/ZeroIsNullMap.java')
-rw-r--r-- | src/org/unitConverter/math/ZeroIsNullMap.java | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/src/org/unitConverter/math/ZeroIsNullMap.java b/src/org/unitConverter/math/ZeroIsNullMap.java new file mode 100644 index 0000000..10e4d52 --- /dev/null +++ b/src/org/unitConverter/math/ZeroIsNullMap.java @@ -0,0 +1,129 @@ +/** + * Copyright (C) 2019 Adrien Hopkins + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package org.unitConverter.math; + +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +/** + * A "wrapper" for an existing map that treats a zero value as equivalent to null. + * + * @author Adrien Hopkins + * @since 2019-10-16 + * @param <T> + * type of key + */ +public final class ZeroIsNullMap<T> extends AbstractMap<T, Integer> { + /** + * Create a ZeroIsNullMap. This method always creates a new map. + * + * @param <T> + * type of key + * @param map + * map to input + * @return map that treats zero as null + * @throws NullPointerException + * if map is null + * @since 2019-10-16 + */ + public static <T> Map<T, Integer> create(final Map<T, Integer> map) { + return new ZeroIsNullMap<>(Objects.requireNonNull(map, "map must not be null.")); + } + + private final Map<T, Integer> map; + + /** + * Creates the {@code ObjectProductMap}. + * + * @param map + * @since 2019-10-16 + */ + private ZeroIsNullMap(final Map<T, Integer> map) { + this.map = map; + } + + @Override + public void clear() { + this.map.clear(); + } + + @Override + public boolean containsKey(final Object key) { + return this.map.containsKey(key) && this.map.get(key) != 0; + } + + @Override + public boolean containsValue(final Object value) { + return this.values().contains(value); + } + + @Override + public Set<Entry<T, Integer>> entrySet() { + final Set<Entry<T, Integer>> entrySet = new HashSet<>(this.map.entrySet()); + entrySet.removeIf(e -> e.getValue() == 0); + return entrySet; + } + + @Override + public Integer get(final Object key) { + final Integer i = this.map.get(key); + if (Objects.equals(i, 0)) + return null; + else + return i; + } + + @Override + public Set<T> keySet() { + final Set<T> keySet = new HashSet<>(this.map.keySet()); + keySet.removeIf(k -> this.map.get(k) == 0); + return keySet; + } + + @Override + public Integer put(final T key, final Integer value) { + if (value != 0) + return this.map.put(key, value); + else + return null; + } + + @Override + public void putAll(final Map<? extends T, ? extends Integer> m) { + for (final T key : m.keySet()) { + this.put(key, m.get(key)); + } + } + + @Override + public Integer remove(final Object key) { + return this.map.remove(key); + } + + @Override + public Collection<Integer> values() { + final List<Integer> values = new ArrayList<>(this.map.values()); + values.removeIf(i -> i == 0); + return values; + } +}
\ No newline at end of file |