summaryrefslogtreecommitdiff
path: root/src/org/unitConverter/math/ZeroIsNullMap.java
diff options
context:
space:
mode:
authorAdrien Hopkins <masterofnumbers17@gmail.com>2019-10-17 16:31:38 -0400
committerAdrien Hopkins <masterofnumbers17@gmail.com>2019-10-17 16:58:55 -0400
commitb491a1d67b513f6a6f549fe6fcb374d731e0beca (patch)
tree843e8933a5e8f6c1b56bcd0025cfeb89ad29e85a /src/org/unitConverter/math/ZeroIsNullMap.java
parent54ab9c05234b09547e2a01b1eab812420c6a3dda (diff)
Changed the ZeroIsNullMap to a more general ConditionalExistenceMap.
Diffstat (limited to 'src/org/unitConverter/math/ZeroIsNullMap.java')
-rw-r--r--src/org/unitConverter/math/ZeroIsNullMap.java129
1 files changed, 0 insertions, 129 deletions
diff --git a/src/org/unitConverter/math/ZeroIsNullMap.java b/src/org/unitConverter/math/ZeroIsNullMap.java
deleted file mode 100644
index 10e4d52..0000000
--- a/src/org/unitConverter/math/ZeroIsNullMap.java
+++ /dev/null
@@ -1,129 +0,0 @@
-/**
- * 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