Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2010 The Android Open Source Project
* Copyright (C) 2012 Google Inc.
*
* 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 io.apptik.json.util;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.NoSuchElementException;
import java.util.Set;
/**
* A map of comparable keys to values. Unlike {@code TreeMap}, this class uses
* insertion order for iteration order. Comparison order is only used as an
* optimization for efficient insertion and removal.
*
*
This implementation was derived from Android 4.1's TreeMap class.
*/
public final class LinkedTreeMap extends AbstractMap implements Serializable {
@SuppressWarnings({"unchecked", "rawtypes"}) // to avoid Comparable>>
private static final Comparator NATURAL_ORDER = new Comparator() {
public int compare(Comparable a, Comparable b) {
return a.compareTo(b);
}
};
Comparator super K> comparator;
Node root;
int size = 0;
int modCount = 0;
// Used to preserve iteration order
final Node header = new Node();
/**
* Create a natural order, empty tree map whose keys must be mutually
* comparable and non-null.
*/
@SuppressWarnings("unchecked") // unsafe! this assumes K is comparable
public LinkedTreeMap() {
this((Comparator super K>) NATURAL_ORDER);
}
/**
* Create a tree map ordered by {@code comparator}. This map's keys may only
* be null if {@code comparator} permits.
*
* @param comparator the comparator to order elements with, or {@code null} to
* use the natural ordering.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
// unsafe! if comparator is null, this assumes K is comparable
public LinkedTreeMap(Comparator super K> comparator) {
this.comparator = comparator != null
? comparator
: (Comparator) NATURAL_ORDER;
}
@Override
public int size() {
return size;
}
@Override
public V get(Object key) {
Node node = findByObject(key);
return node != null ? node.value : null;
}
@Override
public boolean containsKey(Object key) {
return findByObject(key) != null;
}
@Override
public V put(K key, V value) {
if (key == null) {
throw new NullPointerException("key == null");
}
Node created = find(key, true);
V result = created.value;
created.value = value;
return result;
}
@Override
public void clear() {
root = null;
size = 0;
modCount++;
// Clear iteration order
Node header = this.header;
header.next = header.prev = header;
}
@Override
public V remove(Object key) {
Node node = removeInternalByKey(key);
return node != null ? node.value : null;
}
/**
* Returns the node at or adjacent to the given key, creating it if requested.
*
* @throws ClassCastException if {@code key} and the tree's keys aren't
* mutually comparable.
*/
Node find(K key, boolean create) {
Comparator super K> comparator = this.comparator;
Node nearest = root;
int comparison = 0;
if (nearest != null) {
// Micro-optimization: avoid polymorphic calls to Comparator.compare().
@SuppressWarnings("unchecked") // Throws a ClassCastException below if there's trouble.
Comparable