1 /*
2  * Copyright (C) 2017 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 android.location.cts.asn1.supl2.ulp_components;
18 
19 /*
20  */
21 
22 
23 //
24 //
25 import android.location.cts.asn1.base.Asn1Choice;
26 import android.location.cts.asn1.base.Asn1Null;
27 import android.location.cts.asn1.base.Asn1Object;
28 import android.location.cts.asn1.base.Asn1Tag;
29 import android.location.cts.asn1.base.BitStream;
30 import android.location.cts.asn1.base.BitStreamReader;
31 import android.location.cts.asn1.base.ChoiceComponent;
32 import android.location.cts.asn1.supl2.ver2_ulp_components.Ver2_CellInfo_extension;
33 import com.google.common.collect.ImmutableList;
34 import java.nio.ByteBuffer;
35 import java.util.Collection;
36 import java.util.HashMap;
37 import java.util.Map;
38 import javax.annotation.Nullable;
39 
40 
41 /**
42  */
43 public  class CellInfo extends Asn1Choice {
44   //
45 
46   private static final Asn1Tag TAG_CellInfo
47       = Asn1Tag.fromClassAndNumber(-1, -1);
48 
49   private static final Map<Asn1Tag, Select> tagToSelection = new HashMap<>();
50 
51   private boolean extension;
52   private ChoiceComponent selection;
53   private Asn1Object element;
54 
55   static {
56     for (Select select : Select.values()) {
57       for (Asn1Tag tag : select.getPossibleFirstTags()) {
58         Select select0;
59         if ((select0 = tagToSelection.put(tag, select)) != null) {
60           throw new IllegalStateException(
61             "CellInfo: " + tag + " maps to both " + select0 + " and " + select);
62         }
63       }
64     }
65   }
66 
CellInfo()67   public CellInfo() {
68     super();
69   }
70 
71   @Override
72   @Nullable
getTag()73   protected Asn1Tag getTag() {
74     return TAG_CellInfo;
75   }
76 
77   @Override
isTagImplicit()78   protected boolean isTagImplicit() {
79     return true;
80   }
81 
getPossibleFirstTags()82   public static Collection<Asn1Tag> getPossibleFirstTags() {
83     if (TAG_CellInfo != null) {
84       return ImmutableList.of(TAG_CellInfo);
85     } else {
86       return tagToSelection.keySet();
87     }
88   }
89 
90   /**
91    * Creates a new CellInfo from encoded stream.
92    */
fromPerUnaligned(byte[] encodedBytes)93   public static CellInfo fromPerUnaligned(byte[] encodedBytes) {
94     CellInfo result = new CellInfo();
95     result.decodePerUnaligned(new BitStreamReader(encodedBytes));
96     return result;
97   }
98 
99   /**
100    * Creates a new CellInfo from encoded stream.
101    */
fromPerAligned(byte[] encodedBytes)102   public static CellInfo fromPerAligned(byte[] encodedBytes) {
103     CellInfo result = new CellInfo();
104     result.decodePerAligned(new BitStreamReader(encodedBytes));
105     return result;
106   }
107 
108 
109 
hasExtensionValue()110   @Override protected boolean hasExtensionValue() {
111     return extension;
112   }
113 
getSelectionOrdinal()114   @Override protected Integer getSelectionOrdinal() {
115     return selection.ordinal();
116   }
117 
118   @Nullable
119   @Override
getSelectedComponent()120   protected ChoiceComponent getSelectedComponent() {
121     return selection;
122   }
123 
getOptionCount()124   @Override protected int getOptionCount() {
125     if (hasExtensionValue()) {
126       return Extend.values().length;
127     }
128     return Select.values().length;
129   }
130 
createAndSetValue(boolean isExtensionValue, int ordinal)131   protected Asn1Object createAndSetValue(boolean isExtensionValue,
132                                          int ordinal) {
133     extension = isExtensionValue;
134     if (isExtensionValue) {
135       selection = Extend.values()[ordinal];
136     } else {
137       selection = Select.values()[ordinal];
138     }
139     element = selection.createElement();
140     return element;
141   }
142 
createAndSetValue(Asn1Tag tag)143   @Override protected ChoiceComponent createAndSetValue(Asn1Tag tag) {
144     Select select = tagToSelection.get(tag);
145     if (select == null) {
146       throw new IllegalArgumentException("Unknown selection tag: " + tag);
147     }
148     element = select.createElement();
149     selection = select;
150     extension = false;
151     return select;
152   }
153 
isExtensible()154   @Override protected boolean isExtensible() {
155     return true;
156   }
157 
getValue()158   @Override protected Asn1Object getValue() {
159     return element;
160   }
161 
162 
163   private static enum Select implements ChoiceComponent {
164 
165     $GsmCell(Asn1Tag.fromClassAndNumber(2, 0),
166         true) {
167       @Override
createElement()168       public Asn1Object createElement() {
169         return new GsmCellInformation();
170       }
171 
172       @Override
getPossibleFirstTags()173       Collection<Asn1Tag> getPossibleFirstTags() {
174         return tag == null ? GsmCellInformation.getPossibleFirstTags() : ImmutableList.of(tag);
175       }
176 
177       @Override
elementIndentedString(Asn1Object element, String indent)178       String elementIndentedString(Asn1Object element, String indent) {
179         return toString() + " : " + element.toIndentedString(indent);
180       }
181     },
182 
183     $WcdmaCell(Asn1Tag.fromClassAndNumber(2, 1),
184         true) {
185       @Override
createElement()186       public Asn1Object createElement() {
187         return new WcdmaCellInformation();
188       }
189 
190       @Override
getPossibleFirstTags()191       Collection<Asn1Tag> getPossibleFirstTags() {
192         return tag == null ? WcdmaCellInformation.getPossibleFirstTags() : ImmutableList.of(tag);
193       }
194 
195       @Override
elementIndentedString(Asn1Object element, String indent)196       String elementIndentedString(Asn1Object element, String indent) {
197         return toString() + " : " + element.toIndentedString(indent);
198       }
199     },
200 
201     $CdmaCell(Asn1Tag.fromClassAndNumber(2, 2),
202         true) {
203       @Override
createElement()204       public Asn1Object createElement() {
205         return new CdmaCellInformation();
206       }
207 
208       @Override
getPossibleFirstTags()209       Collection<Asn1Tag> getPossibleFirstTags() {
210         return tag == null ? CdmaCellInformation.getPossibleFirstTags() : ImmutableList.of(tag);
211       }
212 
213       @Override
elementIndentedString(Asn1Object element, String indent)214       String elementIndentedString(Asn1Object element, String indent) {
215         return toString() + " : " + element.toIndentedString(indent);
216       }
217     },
218 
219     ;
220 
221     @Nullable final Asn1Tag tag;
222     final boolean isImplicitTagging;
223 
Select(@ullable Asn1Tag tag, boolean isImplicitTagging)224     Select(@Nullable Asn1Tag tag, boolean isImplicitTagging) {
225       this.tag = tag;
226       this.isImplicitTagging = isImplicitTagging;
227     }
228 
229     @Override
createElement()230     public Asn1Object createElement() {
231       throw new IllegalStateException("Select template error");
232     }
233 
234     @Override
235     @Nullable
getTag()236     public Asn1Tag getTag() {
237       return tag;
238     }
239 
240     @Override
isImplicitTagging()241     public boolean isImplicitTagging() {
242       return isImplicitTagging;
243     }
244 
getPossibleFirstTags()245     abstract Collection<Asn1Tag> getPossibleFirstTags();
246 
elementIndentedString(Asn1Object element, String indent)247     abstract String elementIndentedString(Asn1Object element, String indent);
248   }
249 
250 
251 
isGsmCell()252   public boolean isGsmCell() {
253     return !hasExtensionValue() && Select.$GsmCell == selection;
254   }
255 
256   /**
257    * @throws {@code IllegalStateException} if {@code !isGsmCell}.
258    */
259   @SuppressWarnings("unchecked")
getGsmCell()260   public GsmCellInformation getGsmCell() {
261     if (!isGsmCell()) {
262       throw new IllegalStateException("CellInfo value not a GsmCell");
263     }
264     return (GsmCellInformation) element;
265   }
266 
setGsmCell(GsmCellInformation selected)267   public void setGsmCell(GsmCellInformation selected) {
268     selection = Select.$GsmCell;
269     extension = false;
270     element = selected;
271   }
272 
setGsmCellToNewInstance()273   public GsmCellInformation setGsmCellToNewInstance() {
274       GsmCellInformation element = new GsmCellInformation();
275       setGsmCell(element);
276       return element;
277   }
278 
279 
280 
isWcdmaCell()281   public boolean isWcdmaCell() {
282     return !hasExtensionValue() && Select.$WcdmaCell == selection;
283   }
284 
285   /**
286    * @throws {@code IllegalStateException} if {@code !isWcdmaCell}.
287    */
288   @SuppressWarnings("unchecked")
getWcdmaCell()289   public WcdmaCellInformation getWcdmaCell() {
290     if (!isWcdmaCell()) {
291       throw new IllegalStateException("CellInfo value not a WcdmaCell");
292     }
293     return (WcdmaCellInformation) element;
294   }
295 
setWcdmaCell(WcdmaCellInformation selected)296   public void setWcdmaCell(WcdmaCellInformation selected) {
297     selection = Select.$WcdmaCell;
298     extension = false;
299     element = selected;
300   }
301 
setWcdmaCellToNewInstance()302   public WcdmaCellInformation setWcdmaCellToNewInstance() {
303       WcdmaCellInformation element = new WcdmaCellInformation();
304       setWcdmaCell(element);
305       return element;
306   }
307 
308 
309 
isCdmaCell()310   public boolean isCdmaCell() {
311     return !hasExtensionValue() && Select.$CdmaCell == selection;
312   }
313 
314   /**
315    * @throws {@code IllegalStateException} if {@code !isCdmaCell}.
316    */
317   @SuppressWarnings("unchecked")
getCdmaCell()318   public CdmaCellInformation getCdmaCell() {
319     if (!isCdmaCell()) {
320       throw new IllegalStateException("CellInfo value not a CdmaCell");
321     }
322     return (CdmaCellInformation) element;
323   }
324 
setCdmaCell(CdmaCellInformation selected)325   public void setCdmaCell(CdmaCellInformation selected) {
326     selection = Select.$CdmaCell;
327     extension = false;
328     element = selected;
329   }
330 
setCdmaCellToNewInstance()331   public CdmaCellInformation setCdmaCellToNewInstance() {
332       CdmaCellInformation element = new CdmaCellInformation();
333       setCdmaCell(element);
334       return element;
335   }
336 
337 
338   private static enum Extend implements ChoiceComponent {
339 
340     $Ver2_CellInfo_extension(Asn1Tag.fromClassAndNumber(2, 3),
341         true) {
342       @Override
createElement()343       public Asn1Object createElement() {
344         return new Ver2_CellInfo_extension();
345       }
346 
347       @Override
348       @SuppressWarnings("unchecked")
elementIndentedString(Asn1Object element, String indent)349       String elementIndentedString(Asn1Object element, String indent) {
350         return toString() + " : " + ((Ver2_CellInfo_extension) element).toIndentedString(indent);
351       }
352     },
353 
354     ;
355     @Nullable private final Asn1Tag tag;
356     private final boolean isImplicitTagging;
357 
Extend(@ullable Asn1Tag tag, boolean isImplicitTagging)358     Extend(@Nullable Asn1Tag tag, boolean isImplicitTagging) {
359       this.tag = tag;
360       this.isImplicitTagging = isImplicitTagging;
361     }
362 
createElement()363     public Asn1Object createElement() {
364       throw new IllegalStateException("Extend template error");
365     }
366 
367     @Override
368     @Nullable
getTag()369     public Asn1Tag getTag() {
370       return tag;
371     }
372 
373     @Override
isImplicitTagging()374     public boolean isImplicitTagging() {
375       return isImplicitTagging;
376     }
377 
elementIndentedString(Asn1Object element, String indent)378     String elementIndentedString(Asn1Object element, String indent) {
379       throw new IllegalStateException("Extend template error");
380     }
381   }
382 
383 
384 
isExtensionVer2_CellInfo_extension()385   public boolean isExtensionVer2_CellInfo_extension() {
386     return hasExtensionValue() && Extend.$Ver2_CellInfo_extension == selection;
387   }
388 
389   /**
390    * @throws {@code IllegalStateException} if {@code !isVer2_CellInfo_extension}.
391    */
392   @SuppressWarnings("unchecked")
getExtensionVer2_CellInfo_extension()393   public Ver2_CellInfo_extension getExtensionVer2_CellInfo_extension() {
394     if (!isExtensionVer2_CellInfo_extension()) {
395       throw new IllegalStateException("CellInfo value not a Ver2_CellInfo_extension");
396     }
397     return (Ver2_CellInfo_extension) element;
398   }
399 
setExtensionVer2_CellInfo_extension(Ver2_CellInfo_extension selected)400   public void setExtensionVer2_CellInfo_extension(Ver2_CellInfo_extension selected) {
401     selection = Extend.$Ver2_CellInfo_extension;
402     extension = true;
403     element = selected;
404   }
405 
setExtensionVer2_CellInfo_extensionToNewInstance()406   public void setExtensionVer2_CellInfo_extensionToNewInstance() {
407       Ver2_CellInfo_extension element = new Ver2_CellInfo_extension();
408       setExtensionVer2_CellInfo_extension(element);
409   }
410 
411 
encodePerUnaligned()412   @Override public Iterable<BitStream> encodePerUnaligned() {
413     return super.encodePerUnaligned();
414   }
415 
encodePerAligned()416   @Override public Iterable<BitStream> encodePerAligned() {
417     return super.encodePerAligned();
418   }
419 
decodePerUnaligned(BitStreamReader reader)420   @Override public void decodePerUnaligned(BitStreamReader reader) {
421     super.decodePerUnaligned(reader);
422   }
423 
decodePerAligned(BitStreamReader reader)424   @Override public void decodePerAligned(BitStreamReader reader) {
425     super.decodePerAligned(reader);
426   }
427 
toString()428   @Override public String toString() {
429     return toIndentedString("");
430   }
431 
elementIndentedString(String indent)432   private String elementIndentedString(String indent) {
433     if (element == null) {
434       return "null;\n";
435     }
436     if (extension) {
437       return Extend.values()[selection.ordinal()]
438           .elementIndentedString(element, indent + "  ");
439     } else {
440       return Select.values()[selection.ordinal()]
441           .elementIndentedString(element, indent + "  ");
442     }
443   }
444 
toIndentedString(String indent)445   public String toIndentedString(String indent) {
446     return "CellInfo = " + elementIndentedString(indent) + indent + ";\n";
447   }
448 }
449