Debian Codegraph @

layoutlib-api/src/main/java/com/android/ide/common/rendering/api/PluralsResourceValue.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.List;
23
24 /**
25 * Represents an android plurals resource
26 */
27 public class PluralsResourceValue extends ResourceValue {
28
29 private final List<String> mQuantities = new ArrayList<String>();
30
31 private final List<String> mValues = new ArrayList<String>();
32
33 /**
34 * @see #PluralsResourceValue(String, boolean, String)
35 */
36 public PluralsResourceValue(String name, boolean isFramework) {
37 this(name, isFramework, null);
38 }
39
40 /**
41 * Constructs a new {@linkplain PluralsResourceValue}
42 *
43 * @param name the name of the array
44 * @param isFramework whether this is a framework resource
45 */
46 public PluralsResourceValue(String name, boolean isFramework, String libraryName) {
47 super(ResourceType.PLURALS, name, isFramework, libraryName);
48 }
49
50 /**
51 * Adds an element into the array
52 */
53 public void addPlural(String quantity, String value) {
54 mQuantities.add(quantity);
55 mValues.add(value);
56 }
57
58 /**
59 * Returns the number of plural string
60 *
61 * @return the element count
62 */
63 public int getPluralsCount() {
64 return mQuantities.size();
65 }
66
67 /**
68 * Returns the quantity at the given index, such as "one", "two", "few", etc.
69 *
70 * @param index the index, which must be in the range [0..getPluralsCount()].
71 * @return the corresponding quantity string
72 */
73 public String getQuantity(int index) {
74 return mQuantities.get(index);
75 }
76
77 /**
78 * Returns the string element at the given index position.
79 *
80 * @param index index, which must be in the range [0..getPluralsCount()].
81 * @return the corresponding element
82 */
83 public String getValue(int index) {
84 return mValues.get(index);
85 }
86
87 /**
88 * Returns the string element for the given quantity
89 *
90 * @param quantity the quantity string, such as "one", "two", "few", etc.
91 * @return the corresponding string value, or null if not defined
92 */
93 public String getValue(String quantity) {
94 assert mQuantities.size() == mValues.size();
95 for (int i = 0, n = mQuantities.size(); i < n; i++) {
96 if (quantity.equals(mQuantities.get(i))) {
97 return mValues.get(i);
98 }
99 }
100
101 return null;
102 }
103
104 @Override
105 public String getValue() {
106 // Clients should normally not call this method on PluralsResourceValues; they should
107 // pick the specific quantity element they want. However, for compatibility with older
108 // layout libs, return the first plurals element's value instead.
109
110 //noinspection VariableNotUsedInsideIf
111 if (mValue == null) {
112 if (!mValues.isEmpty()) {
113 return getValue(0);
114 }
115
116 }
117
118 return mValue;
119 }
120 }