1 /*
2 * Themis: Java Project Framework
3 * Copyright 2012-2026. Tony Washer
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 * use this file except in compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17 package io.github.tonywasher.joceanus.themis.lethe.analysis;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.themis.exc.ThemisDataException;
21
22 /**
23 * Array construct.
24 */
25 public class ThemisAnalysisArray {
26 /**
27 * The varArgs notation.
28 */
29 static final String VARARGS = "...";
30
31 /**
32 * The array notation.
33 */
34 private final String theNotation;
35
36 /**
37 * Constructor.
38 *
39 * @param pLine the line
40 * @throws OceanusException on error
41 */
42 ThemisAnalysisArray(final ThemisAnalysisLine pLine) throws OceanusException {
43 /* If the token is VARARGS */
44 if (VARARGS.equals(pLine.peekNextToken())) {
45 pLine.stripNextToken();
46 theNotation = VARARGS;
47 return;
48 }
49
50 /* Loop while we have an array start */
51 int myDepth = 0;
52 while (pLine.startsWithChar(ThemisAnalysisChar.ARRAY_OPEN)) {
53 pLine.stripStartChar(ThemisAnalysisChar.ARRAY_OPEN);
54 if (!pLine.startsWithChar(ThemisAnalysisChar.ARRAY_CLOSE)) {
55 throw new ThemisDataException("Bad array notation");
56 }
57 pLine.stripStartChar(ThemisAnalysisChar.ARRAY_CLOSE);
58 myDepth++;
59 }
60 pLine.stripLeadingWhiteSpace();
61
62 /* Build the array notation */
63 final StringBuilder myBuilder = new StringBuilder();
64 for (int i = 0; i < myDepth; i++) {
65 myBuilder.append(ThemisAnalysisChar.ARRAY_OPEN).append(ThemisAnalysisChar.ARRAY_CLOSE);
66 }
67 theNotation = myBuilder.toString();
68 }
69
70 /**
71 * Is the line a generic?
72 *
73 * @param pLine the line
74 * @return true/false
75 */
76 static boolean isArray(final ThemisAnalysisLine pLine) {
77 /* If we are started with an ARRAY_OPEN or we have args */
78 return pLine.startsWithChar(ThemisAnalysisChar.ARRAY_OPEN)
79 || VARARGS.equals(pLine.peekNextToken());
80 }
81
82 @Override
83 public String toString() {
84 return theNotation;
85 }
86 }