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    
017    package org.jetbrains.kotlin.util.slicedMap;
018    
019    import org.jetbrains.annotations.NotNull;
020    
021    import java.lang.reflect.Field;
022    import java.lang.reflect.Modifier;
023    
024    public class BasicWritableSlice<K, V> implements WritableSlice<K, V> {
025    
026        public static Void initSliceDebugNames(Class<?> declarationOwner) {
027            for (Field field : declarationOwner.getFields()) {
028                if (!Modifier.isStatic(field.getModifiers())) continue;
029                try {
030                    Object value = field.get(null);
031                    if (value instanceof BasicWritableSlice) {
032                        BasicWritableSlice slice = (BasicWritableSlice) value;
033                        slice.debugName = field.getName();
034                    }
035                } catch (IllegalAccessException e) {
036                    throw new IllegalStateException(e);
037                }
038            }
039            return null;
040        }
041        
042        private String debugName;
043        private final RewritePolicy rewritePolicy;
044        private final boolean isCollective;
045    
046        public BasicWritableSlice(RewritePolicy rewritePolicy) {
047            this(rewritePolicy, false);
048        }
049    
050        public BasicWritableSlice(RewritePolicy rewritePolicy, boolean isCollective) {
051            this.rewritePolicy = rewritePolicy;
052            this.isCollective = isCollective;
053        }
054    
055        @Override
056        public SlicedMapKey<K, V> makeKey(K key) {
057            return new SlicedMapKey<K, V>(this, key);
058        }
059    
060        // True to put, false to skip
061        @Override
062        public boolean check(K key, V value) {
063    //        assert key != null : this + " called with null key";
064            assert value != null : this + " called with null value";
065            return true;
066        }
067    
068        @Override
069        public void afterPut(MutableSlicedMap map, K key, V value) {
070            // Do nothing
071        }
072    
073        @Override
074        public V computeValue(SlicedMap map, K key, V value, boolean valueNotFound) {
075            if (valueNotFound) assert value == null;
076            return value;
077        }
078    
079        @Override
080        public RewritePolicy getRewritePolicy() {
081            return rewritePolicy;
082        }
083    
084        @Override
085        public boolean isCollective() {
086            return isCollective;
087        }
088    
089        public void setDebugName(@NotNull String debugName) {
090            if (this.debugName != null) {
091                throw new IllegalStateException("Debug name already set for " + this);
092            }
093            this.debugName = debugName;
094        }
095    
096        @Override
097        public String toString() {
098            return debugName;
099        }
100    
101        @Override
102        public ReadOnlySlice<K, V> makeRawValueVersion() {
103            return new DelegatingSlice<K, V>(this) {
104                @Override
105                public V computeValue(SlicedMap map, K key, V value, boolean valueNotFound) {
106                    if (valueNotFound) assert value == null;
107                    return value;
108                }
109            };
110        }
111    
112    
113    }