objectos.way.UtilMaps Maven / Gradle / Ivy
Show all versions of objectos.way Show documentation
/*
* Copyright (C) 2022-2023 Objectos Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package objectos.way;
import java.util.AbstractMap;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
/**
* This class provides {@code static} factory methods for the JDK
* {@link java.util.Map} implementations.
*
*
* This class is mainly provided as a convenience for Java Multi-Release
* codebases. In particular codebases that must support versions prior to Java
* 7 and, therefore, cannot use the diamond operator.
*/
final class UtilMaps {
private static class OrderedEntryIterator extends Util.UnmodifiableIterator> {
private final Object[] array;
private int index;
private final int size;
OrderedEntryIterator(Object[] array, int size) {
this.array = array;
this.size = size;
}
@Override
public final boolean hasNext() {
return index < size;
}
@Override
public final Entry next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
else {
Object key;
key = array[index];
index++;
Object value;
value = array[index];
index++;
return newEntry(key, value);
}
}
}
private static class OrderedKeyOrValueIterator extends Util.UnmodifiableIterator {
private final Object[] array;
private int index;
private final int size;
OrderedKeyOrValueIterator(Object[] array, int size, int initialIndex) {
this.array = array;
this.size = size;
index = initialIndex;
}
@Override
public final boolean hasNext() {
return index < size;
}
@SuppressWarnings("unchecked")
@Override
public final K next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
else {
Object key;
key = array[index];
index = index + 2;
return (K) key;
}
}
}
private static class SparseEntryIterator extends Util.UnmodifiableIterator> {
private final Object[] array;
private boolean computed;
private int index;
private Entry next;
SparseEntryIterator(Object[] array) {
this.array = array;
}
@Override
public final boolean hasNext() {
if (!computed) {
while (index < array.length) {
Object key;
key = array[index];
index++;
int valueIndex;
valueIndex = index;
index++;
if (key != null) {
Object value;
value = array[valueIndex];
next = newEntry(key, value);
break;
}
}
computed = true;
}
return next != null;
}
@Override
public final Entry next() {
if (!hasNext()) {
throw new NoSuchElementException();
} else {
Entry result;
result = next;
computed = false;
next = null;
return result;
}
}
}
private static class SparseKeyOrValueIterator extends Util.UnmodifiableIterator {
private final Object[] array;
private boolean computed;
private int index;
private Object next;
SparseKeyOrValueIterator(Object[] array, int initialIndex) {
this.array = array;
index = initialIndex;
}
@Override
public final boolean hasNext() {
if (!computed) {
while (index < array.length) {
var value = array[index];
index = index + 2;
if (value != null) {
next = value;
break;
}
}
computed = true;
}
return next != null;
}
@SuppressWarnings("unchecked")
@Override
public final K next() {
if (!hasNext()) {
throw new NoSuchElementException();
} else {
var result = next;
computed = false;
next = null;
return (K) result;
}
}
}
private UtilMaps() {}
static Util.UnmodifiableIterator> orderedEntryIterator(Object[] array, int size) {
return new OrderedEntryIterator(array, size);
}
static Util.UnmodifiableIterator orderedKeyIterator(Object[] array, int size) {
return new OrderedKeyOrValueIterator(array, size, 0);
}
static Util.UnmodifiableIterator orderedValueIterator(Object[] array, int size) {
return new OrderedKeyOrValueIterator(array, size, 1);
}
static Util.UnmodifiableIterator> sparseEntryIterator(Object[] array) {
return new SparseEntryIterator(array);
}
static Util.UnmodifiableIterator sparseKeyIterator(Object[] array) {
return new SparseKeyOrValueIterator(array, 0);
}
static Util.UnmodifiableIterator sparseValueIterator(Object[] array) {
return new SparseKeyOrValueIterator(array, 1);
}
@SuppressWarnings("unchecked")
private static SimpleEntry newEntry(Object key, Object value) {
K castKey;
castKey = (K) key;
V castValue;
castValue = (V) value;
return new AbstractMap.SimpleEntry(castKey, castValue);
}
}