1 /* 2 * Copyright 2013, Google LLC 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google LLC nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 package com.android.tools.smali.dexlib2.writer.builder; 32 33 import com.android.tools.smali.dexlib2.writer.DexWriter; 34 import com.android.tools.smali.dexlib2.writer.StringSection; 35 36 import javax.annotation.Nonnull; 37 import javax.annotation.Nullable; 38 import java.util.Collection; 39 import java.util.Map.Entry; 40 import java.util.concurrent.ConcurrentHashMap; 41 import java.util.concurrent.ConcurrentMap; 42 43 class BuilderStringPool implements StringSection<BuilderStringReference, BuilderStringReference> { 44 @Nonnull private final ConcurrentMap<String, BuilderStringReference> internedItems = new ConcurrentHashMap<>(); 45 internString(@onnull String string)46 @Nonnull BuilderStringReference internString(@Nonnull String string) { 47 BuilderStringReference ret = internedItems.get(string); 48 if (ret != null) { 49 return ret; 50 } 51 BuilderStringReference stringReference = new BuilderStringReference(string); 52 ret = internedItems.putIfAbsent(string, stringReference); 53 return ret==null?stringReference:ret; 54 } 55 internNullableString(@ullable String string)56 @Nullable BuilderStringReference internNullableString(@Nullable String string) { 57 if (string == null) { 58 return null; 59 } 60 return internString(string); 61 } 62 getNullableItemIndex(@ullable BuilderStringReference key)63 @Override public int getNullableItemIndex(@Nullable BuilderStringReference key) { 64 return key==null? DexWriter.NO_INDEX:key.index; 65 } 66 getItemIndex(@onnull BuilderStringReference key)67 @Override public int getItemIndex(@Nonnull BuilderStringReference key) { 68 return key.index; 69 } 70 hasJumboIndexes()71 @Override public boolean hasJumboIndexes() { 72 return internedItems.size() > 65536; 73 } 74 getItems()75 @Nonnull @Override public Collection<? extends Entry<? extends BuilderStringReference, Integer>> getItems() { 76 return new BuilderMapEntryCollection<BuilderStringReference>(internedItems.values()) { 77 @Override protected int getValue(@Nonnull BuilderStringReference key) { 78 return key.index; 79 } 80 81 @Override protected int setValue(@Nonnull BuilderStringReference key, int value) { 82 int prev = key.index; 83 key.index = value; 84 return prev; 85 } 86 }; 87 } 88 89 @Override public int getItemCount() { 90 return internedItems.size(); 91 } 92 } 93