001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.jetbrains.kotlin.util.userDataHolder.keyFMap;
017    
018    import com.intellij.openapi.util.Key;
019    import org.jetbrains.annotations.NotNull;
020    
021    public class OneElementFMap<V> implements KeyFMap {
022      private final Key myKey;
023      private final V myValue;
024    
025      public OneElementFMap(@NotNull Key key, @NotNull V value) {
026        myKey = key;
027        myValue = value;
028      }
029    
030      @NotNull
031      @Override
032      public <V> KeyFMap plus(@NotNull Key<V> key, @NotNull V value) {
033        if (myKey == key) return new OneElementFMap<V>(key, value);
034        return new PairElementsFMap(myKey, myValue, key, value);
035      }
036    
037      @NotNull
038      @Override
039      public KeyFMap minus(@NotNull Key<?> key) {
040        return key == myKey ? KeyFMap.EMPTY_MAP : this;
041      }
042    
043      @Override
044      public <V> V get(@NotNull Key<V> key) {
045        //noinspection unchecked
046        return myKey == key ? (V)myValue : null;
047      }
048    
049      @NotNull
050      @Override
051      public Key[] getKeys() {
052        return new Key[] { myKey };
053      }
054    
055      @Override
056      public String toString() {
057        return "<" + myKey + " -> " + myValue+">";
058      }
059    
060      @Override
061      public boolean isEmpty() {
062        return false;
063      }
064    
065      public Key getKey() {
066        return myKey;
067      }
068    
069      public V getValue() {
070        return myValue;
071      }
072    
073      @Override
074      public boolean equals(Object o) {
075        if (this == o) return true;
076        if (!(o instanceof OneElementFMap)) return false;
077    
078        OneElementFMap map = (OneElementFMap)o;
079    
080        if (myKey != map.myKey) return false;
081        if (!myValue.equals(map.myValue)) return false;
082    
083        return true;
084      }
085    
086      @Override
087      public int hashCode() {
088        int result = myKey.hashCode();
089        result = 31 * result + myValue.hashCode();
090        return result;
091      }
092    }