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.data;
18  
19  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
20  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter.OceanusDataFormatterExtension;
21  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataList;
22  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataMap;
23  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataObjectFormat;
24  
25  /**
26   * Generic Data object formatter.
27   *
28   * @author Tony Washer
29   */
30  public class MetisDataFormatter
31          implements OceanusDataFormatterExtension {
32      /**
33       * The formatter.
34       */
35      private final OceanusDataFormatter theFormatter;
36  
37      /**
38       * Constructor.
39       *
40       * @param pFormatter the formatter
41       */
42      public MetisDataFormatter(final OceanusDataFormatter pFormatter) {
43          theFormatter = pFormatter;
44      }
45  
46      @Override
47      public String formatObject(final Object pValue) {
48          /* Handle maps and lists */
49          if (pValue instanceof MetisDataMap<?, ?> myMap) {
50              return formatValue(pValue, myMap.size());
51          }
52          if (pValue instanceof MetisDataList<?> myList) {
53              return formatValue(pValue, myList.size());
54          }
55  
56          /* Format other values */
57          return formatValue(pValue);
58      }
59  
60      /**
61       * Format the value.
62       *
63       * @param pValue the value
64       * @return the formatted value
65       */
66      private String formatValue(final Object pValue) {
67          /* Handle delta class */
68          if (pValue instanceof MetisDataDelta myDelta) {
69              return formatObject(myDelta.getObject());
70          }
71  
72          /* Handle ones that we can directly format */
73          if (pValue instanceof MetisDataObjectFormat myFormat) {
74              return myFormat.formatObject(theFormatter);
75          }
76  
77          /* Not supported */
78          return null;
79      }
80  
81      /**
82       * Format the list/map.
83       *
84       * @param pValue the value
85       * @param pSize  the size
86       * @return the formatted value
87       */
88      private String formatValue(final Object pValue,
89                                 final int pSize) {
90          /* Format value normally */
91          final String myValue = formatValue(pValue);
92  
93          /* Return modified value */
94          return myValue
95                  + '('
96                  + pSize
97                  + ')';
98      }
99  }