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 org.apache.reef.tang.BindLocation; 022import org.apache.reef.tang.implementation.StackBindLocation; 023 024import java.util.Collection; 025import java.util.Map; 026import java.util.Set; 027 028public final class TracingMonotonicTreeMap<K, V> implements TracingMonotonicMap<K, V> { 029 private final MonotonicTreeMap<K, EntryImpl> innerMap = new MonotonicTreeMap<>(); 030 031 @Override 032 public void clear() { 033 innerMap.clear(); 034 } 035 036 @Override 037 public boolean containsKey(final Object key) { 038 return innerMap.containsKey(key); 039 } 040 041 @Override 042 public boolean containsValue(final Object value) { 043 throw new UnsupportedOperationException(); 044 } 045 046 @Override 047 public Set<Entry<K, V>> entrySet() { 048 throw new UnsupportedOperationException(); 049 } 050 051 @Override 052 public V get(final Object key) { 053 final EntryImpl ret = innerMap.get(key); 054 return ret != null ? ret.getKey() : null; 055 } 056 057 @Override 058 public boolean isEmpty() { 059 return innerMap.isEmpty(); 060 } 061 062 @Override 063 public Set<K> keySet() { 064 return innerMap.keySet(); 065 } 066 067 @Override 068 public V put(final K key, final V value) { 069 final EntryImpl ret = innerMap.put(key, new EntryImpl(value, new StackBindLocation())); 070 return ret != null ? ret.getKey() : null; 071 } 072 073 @Override 074 public void putAll(final Map<? extends K, ? extends V> m) { 075 throw new UnsupportedOperationException(); 076 } 077 078 @Override 079 public V remove(final Object key) { 080 throw new UnsupportedOperationException(); 081 } 082 083 @Override 084 public int size() { 085 return innerMap.size(); 086 } 087 088 @Override 089 public Collection<V> values() { 090 throw new UnsupportedOperationException(); 091 } 092 093 @Override 094 public boolean equals(final Object o) { 095 if (this == o) { 096 return true; 097 } 098 if (o == null || getClass() != o.getClass()) { 099 return false; 100 } 101 102 final TracingMonotonicTreeMap that = (TracingMonotonicTreeMap) o; 103 104 if (innerMap != null ? !innerMap.equals(that.innerMap) : that.innerMap != null) { 105 return false; 106 } 107 108 return true; 109 } 110 111 @Override 112 public int hashCode() { 113 return innerMap != null ? innerMap.hashCode() : 0; 114 } 115 116 private class EntryImpl implements Map.Entry<V, BindLocation> { 117 private final V key; 118 private final BindLocation value; 119 120 EntryImpl(final V key, final BindLocation value) { 121 this.key = key; 122 this.value = value; 123 } 124 125 @Override 126 public V getKey() { 127 return key; 128 } 129 130 @Override 131 public BindLocation getValue() { 132 return value; 133 } 134 135 @Override 136 @SuppressWarnings("checkstyle:hiddenfield") 137 public BindLocation setValue(final BindLocation value) { 138 throw new UnsupportedOperationException(); 139 } 140 141 @Override 142 public String toString() { 143 return "[" + key + "] set by " + value; 144 } 145 146 } 147}