Debian Codegraph @

layoutlib-api/src/main/java/com/android/ide/common/rendering/api/ArrayResourceValue.java

raw
1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.ide.common.rendering.api;
18
19 import com.android.resources.ResourceType;
20
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24
25 /**
26 * Represents an android array resource with a name and a list of children {@link
27 * com.android.ide.common.rendering.api.ResourceValue} items, one for array element.
28 */
29 public class ArrayResourceValue extends ResourceValue implements Iterable<String> {
30
31 private final List<String> mItems = new ArrayList<String>();
32
33 /**
34 * @see #ArrayResourceValue(String, boolean, String)
35 */
36 public ArrayResourceValue(String name, boolean isFramework) {
37 this(name, isFramework, null);
38 }
39
40 /**
41 * Constructs a new {@linkplain ArrayResourceValue}
42 *
43 * @param name the name of the array
44 * @param isFramework whether this is a framework resource
45 * @param libraryName the name of the library where the resource was found
46 */
47 public ArrayResourceValue(String name, boolean isFramework, String libraryName) {
48 super(ResourceType.ARRAY, name, isFramework, libraryName);
49 }
50
51 /**
52 * Adds an element into the array
53 */
54 public void addElement(String value) {
55 mItems.add(value);
56 }
57
58 /**
59 * Returns the number of elements in this array
60 *
61 * @return the element count
62 */
63 public int getElementCount() {
64 return mItems.size();
65 }
66
67 /**
68 * Returns the array element value at the given index position.
69 *
70 * @param index index, which must be in the range [0..getElementCount()].
71 * @return the corresponding element
72 */
73 public String getElement(int index) {
74 return mItems.get(index);
75 }
76
77 /**
78 * Returns an iterator over the resource values
79 */
80 @Override
81 public Iterator<String> iterator() {
82 return mItems.iterator();
83 }
84
85 /**
86 * Returns the index of the element to pick by default if a client
87 * of layoutlib asks for the {@link #getValue()} rather than the more
88 * specific {@linkplain ArrayResourceValue} iteration methods
89 */
90 protected int getDefaultIndex() {
91 return 0;
92 }
93
94 @Override
95 public String getValue() {
96 // Clients should normally not call this method on ArrayResourceValues; they should
97 // pick the specific array element they want. However, for compatibility with older
98 // layout libs, return the first array element's value instead.
99
100 //noinspection VariableNotUsedInsideIf
101 if (mValue == null) {
102 if (!mItems.isEmpty()) {
103 return mItems.get(getDefaultIndex());
104 }
105 }
106
107 return mValue;
108 }
109 }