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> extends AbstractWritableSlice<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 super("<BasicWritableSlice>"); 052 053 this.rewritePolicy = rewritePolicy; 054 this.isCollective = isCollective; 055 } 056 057 // True to put, false to skip 058 @Override 059 public boolean check(K key, V value) { 060 // assert key != null : this + " called with null key"; 061 assert value != null : this + " called with null value"; 062 return true; 063 } 064 065 @Override 066 public void afterPut(MutableSlicedMap map, K key, V value) { 067 // Do nothing 068 } 069 070 @Override 071 public V computeValue(SlicedMap map, K key, V value, boolean valueNotFound) { 072 if (valueNotFound) assert value == null; 073 return value; 074 } 075 076 @Override 077 public RewritePolicy getRewritePolicy() { 078 return rewritePolicy; 079 } 080 081 @Override 082 public boolean isCollective() { 083 return isCollective; 084 } 085 086 public void setDebugName(@NotNull String debugName) { 087 if (this.debugName != null) { 088 throw new IllegalStateException("Debug name already set for " + this); 089 } 090 this.debugName = debugName; 091 } 092 093 @Override 094 public String toString() { 095 return debugName; 096 } 097 098 @Override 099 public ReadOnlySlice<K, V> makeRawValueVersion() { 100 return new DelegatingSlice<K, V>(this) { 101 @Override 102 public V computeValue(SlicedMap map, K key, V value, boolean valueNotFound) { 103 if (valueNotFound) assert value == null; 104 return value; 105 } 106 }; 107 } 108 109 110 }