001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.tang.util; 020 021import java.util.Collection; 022import java.util.Map; 023import java.util.Set; 024import java.util.TreeMap; 025 026public final class MonotonicTreeMap<T, U> implements Map<T, U> { 027 private static final long serialVersionUID = 1L; 028 029 private final TreeMap<T, U> innerMap = new TreeMap<>(); 030 031 032 @Override 033 public int size() { 034 return innerMap.size(); 035 } 036 037 @Override 038 public boolean isEmpty() { 039 return innerMap.isEmpty(); 040 } 041 042 @Override 043 public boolean containsKey(final Object o) { 044 return innerMap.containsKey(o); 045 } 046 047 @Override 048 public boolean containsValue(final Object o) { 049 return innerMap.containsValue(o); 050 } 051 052 @Override 053 public U get(final Object o) { 054 return innerMap.get(o); 055 } 056 057 @Override 058 public U put(final T key, final U value) { 059 final U old = innerMap.get(key); 060 if (old != null) { 061 throw new IllegalArgumentException("Attempt to re-add: [" + key 062 + "]\n old value: " + old + " new value " + value); 063 } 064 return innerMap.put(key, value); 065 } 066 067 @Override 068 public void putAll(final Map<? extends T, ? extends U> m) { 069 throw new UnsupportedOperationException(); 070 } 071 072 @Override 073 public void clear() { 074 throw new UnsupportedOperationException(); 075 } 076 077 @Override 078 public Set<T> keySet() { 079 return innerMap.keySet(); 080 } 081 082 @Override 083 public Collection<U> values() { 084 return innerMap.values(); 085 } 086 087 @Override 088 public Set<Entry<T, U>> entrySet() { 089 return innerMap.entrySet(); 090 } 091 092 @Override 093 public U remove(final Object o) { 094 throw new UnsupportedOperationException(); 095 } 096 097 @Override 098 public boolean equals(final Object o) { 099 if (this == o) { 100 return true; 101 } 102 if (o == null || getClass() != o.getClass()) { 103 return false; 104 } 105 106 final MonotonicTreeMap that = (MonotonicTreeMap) o; 107 108 if (!innerMap.equals(that.innerMap)) { 109 return false; 110 } 111 112 return true; 113 } 114 115 @Override 116 public int hashCode() { 117 return innerMap.hashCode(); 118 } 119}