1/*
2 * Copyright 2013, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32package org.jf.dexlib2.writer.builder;
33
34import com.google.common.base.Function;
35import com.google.common.collect.ImmutableList;
36import com.google.common.collect.Iterables;
37import com.google.common.collect.Maps;
38import org.jf.dexlib2.writer.DexWriter;
39import org.jf.dexlib2.writer.TypeListSection;
40
41import javax.annotation.Nonnull;
42import javax.annotation.Nullable;
43import java.util.Collection;
44import java.util.List;
45import java.util.Map.Entry;
46import java.util.concurrent.ConcurrentMap;
47
48class BuilderTypeListPool implements TypeListSection<BuilderTypeReference, BuilderTypeList> {
49    @Nonnull private final BuilderContext context;
50    @Nonnull private final ConcurrentMap<List<? extends CharSequence>, BuilderTypeList> internedItems =
51            Maps.newConcurrentMap();
52
53    BuilderTypeListPool(@Nonnull BuilderContext context) {
54        this.context = context;
55    }
56
57    @Nonnull public BuilderTypeList internTypeList(@Nullable List<? extends CharSequence> types) {
58        if (types == null || types.size() == 0) {
59            return BuilderTypeList.EMPTY;
60        }
61
62        BuilderTypeList ret = internedItems.get(types);
63        if (ret != null) {
64            return ret;
65        }
66
67        BuilderTypeList typeList = new BuilderTypeList(
68                ImmutableList.copyOf(Iterables.transform(types, new Function<CharSequence, BuilderTypeReference>() {
69                    @Nonnull @Override public BuilderTypeReference apply(CharSequence input) {
70                        return context.typePool.internType(input.toString());
71                    }
72                })));
73
74        ret = internedItems.putIfAbsent(typeList, typeList);
75        return ret==null?typeList:ret;
76    }
77
78    @Override public int getNullableItemOffset(@Nullable BuilderTypeList key) {
79        return (key==null||key.size()==0)?DexWriter.NO_OFFSET:key.offset;
80    }
81
82    @Nonnull @Override
83    public Collection<? extends BuilderTypeReference> getTypes(@Nullable BuilderTypeList key) {
84        return key==null?BuilderTypeList.EMPTY:key.types;
85    }
86
87    @Override public int getItemOffset(@Nonnull BuilderTypeList key) {
88        return key.offset;
89    }
90
91    @Nonnull @Override public Collection<? extends Entry<? extends BuilderTypeList, Integer>> getItems() {
92        return new BuilderMapEntryCollection<BuilderTypeList>(internedItems.values()) {
93            @Override protected int getValue(@Nonnull BuilderTypeList key) {
94                return key.offset;
95            }
96
97            @Override protected int setValue(@Nonnull BuilderTypeList key, int value) {
98                int prev = key.offset;
99                key.offset = value;
100                return prev;
101            }
102        };
103    }
104}
105