summaryrefslogtreecommitdiff
path: root/src/org/unitConverter/math/ZeroIsNullMap.java
blob: 10e4d526c20511add469909955ff09fb5c7583d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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;
	}
}