View Javadoc
1   /*
2    * Metis: Java Data 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.metis.viewer;
18  
19  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
20  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataList;
21  import io.github.tonywasher.joceanus.metis.data.MetisDataResource;
22  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
23  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
24  
25  import java.util.ArrayList;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  /**
30   * Report-able error list.
31   */
32  public class MetisViewerErrorList
33          implements MetisFieldItem, MetisDataList<MetisViewerExceptionWrapper> {
34      /**
35       * Local Report fields.
36       */
37      private static final MetisFieldSet<MetisViewerErrorList> FIELD_DEFS = MetisFieldSet.newFieldSet(MetisViewerErrorList.class);
38  
39      /*
40       * Declare Fields.
41       */
42      static {
43          FIELD_DEFS.declareLocalField(MetisDataResource.LIST_SIZE, MetisViewerErrorList::size);
44      }
45  
46      /**
47       * The list.
48       */
49      private final List<MetisViewerExceptionWrapper> theList;
50  
51      /**
52       * Constructor.
53       */
54      public MetisViewerErrorList() {
55          theList = new ArrayList<>();
56      }
57  
58      @Override
59      public MetisFieldSet<MetisViewerErrorList> getDataFieldSet() {
60          return FIELD_DEFS;
61      }
62  
63      @Override
64      public String formatObject(final OceanusDataFormatter pFormatter) {
65          return getDataFieldSet().getName();
66      }
67  
68      /**
69       * Add elements.
70       *
71       * @param pValues the list of values to add
72       */
73      public void addList(final MetisViewerErrorList pValues) {
74          /* Loop through the new values */
75          final Iterator<MetisViewerExceptionWrapper> myIterator = pValues.iterator();
76          while (myIterator.hasNext()) {
77              /* Add the value */
78              theList.add(myIterator.next());
79          }
80      }
81  
82      @Override
83      public List<MetisViewerExceptionWrapper> getUnderlyingList() {
84          return theList;
85      }
86  
87      /**
88       * Obtain the first error.
89       *
90       * @return the first error (or null)
91       */
92      public MetisViewerExceptionWrapper getFirst() {
93          return isEmpty()
94                  ? null
95                  : theList.get(0);
96      }
97  }