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.field;
18  
19  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
20  import io.github.tonywasher.joceanus.metis.data.MetisDataDelta;
21  import io.github.tonywasher.joceanus.metis.data.MetisDataDifference;
22  import io.github.tonywasher.joceanus.metis.data.MetisDataResource;
23  
24  import java.util.Iterator;
25  
26  /**
27   * Set of dataValue Deltas.
28   */
29  public class MetisFieldVersionDelta
30          implements MetisFieldItem {
31      /**
32       * FieldSet definitions.
33       */
34      private static final MetisFieldSet<MetisFieldVersionDelta> FIELD_SET = MetisFieldSet.newFieldSet(MetisFieldVersionDelta.class);
35  
36      /*
37       * Initialise the Version Field.
38       */
39      static {
40          FIELD_SET.declareLocalField(MetisDataResource.DATA_VERSION, MetisFieldVersionDelta::getVersion);
41      }
42  
43      /**
44       * Old ValueSet.
45       */
46      private final MetisFieldVersionValues theOldSet;
47  
48      /**
49       * New ValueSet.
50       */
51      private final MetisFieldVersionValues theNewSet;
52  
53      /**
54       * Local fieldSet.
55       */
56      private MetisFieldSet<MetisFieldVersionDelta> theLocalFields;
57  
58      /**
59       * Constructor.
60       *
61       * @param pNew the new valueSet.
62       * @param pOld the old valueSet.
63       */
64      protected MetisFieldVersionDelta(final MetisFieldVersionValues pNew,
65                                       final MetisFieldVersionValues pOld) {
66          /* Store parameters */
67          theOldSet = pOld;
68          theNewSet = pNew;
69      }
70  
71      /**
72       * Obtain the version.
73       *
74       * @return the version
75       */
76      public int getVersion() {
77          return theOldSet.getVersion();
78      }
79  
80      @Override
81      public String formatObject(final OceanusDataFormatter pFormatter) {
82          /* Initialise number of differences */
83          int myNumDiffs = theOldSet.isDeletion() == theNewSet.isDeletion()
84                  ? 0
85                  : 1;
86  
87          /* Loop through the fields */
88          final Iterator<MetisFieldDef> myIterator = getDataFieldSet().fieldIterator();
89          while (myIterator.hasNext()) {
90              /* Access Field */
91              final MetisFieldDef myField = myIterator.next();
92  
93              /* Skip if the field is not versioned */
94              if (!(myField instanceof MetisFieldVersionedDef)) {
95                  continue;
96              }
97  
98              /* Obtain the difference */
99              final Object myObject = theOldSet.getValue(myField);
100             final MetisDataDifference myDifference = MetisDataDifference.difference(myObject, theNewSet.getValue(myField));
101 
102             /* If there is a difference */
103             if (!myDifference.isIdentical()) {
104                 /* Increment the differences */
105                 myNumDiffs++;
106             }
107         }
108 
109         /* Return the number of differences */
110         return MetisFieldVersionDelta.class.getSimpleName()
111                 + "("
112                 + myNumDiffs
113                 + ")";
114     }
115 
116     @Override
117     public MetisFieldSetDef getDataFieldSet() {
118         if (theLocalFields == null) {
119             theLocalFields = buildLocalFieldSet();
120         }
121         return theLocalFields;
122     }
123 
124     /**
125      * Build localFieldSet.
126      *
127      * @return the fieldSet
128      */
129     private MetisFieldSet<MetisFieldVersionDelta> buildLocalFieldSet() {
130         /* Access the owning item fields */
131         final MetisFieldItem myItem = theOldSet.getItem();
132         final MetisFieldSetDef myFields = myItem.getDataFieldSet();
133 
134         /* Allocate new local fields */
135         final MetisFieldSet<MetisFieldVersionDelta> myLocal = MetisFieldSet.newFieldSet(this);
136 
137         /* If we have a change in deletion status */
138         if (theOldSet.isDeletion() != theNewSet.isDeletion()) {
139             /* Declare the deleted field */
140             myLocal.declareLocalField(MetisDataResource.DATA_DELETED.getValue(), f -> new MetisDataDelta(theOldSet.isDeletion(), MetisDataDifference.DIFFERENT));
141         }
142 
143         /* Loop through the fields */
144         final Iterator<MetisFieldDef> myIterator = myFields.fieldIterator();
145         while (myIterator.hasNext()) {
146             /* Access Field */
147             final MetisFieldDef myField = myIterator.next();
148 
149             /* Skip if the field is not versioned */
150             if (!(myField instanceof MetisFieldVersionedDef)) {
151                 continue;
152             }
153 
154             /* Obtain the difference */
155             final Object myObject = theOldSet.getValue(myField);
156             final MetisDataDifference myDifference = MetisDataDifference.difference(myObject, theNewSet.getValue(myField));
157 
158             /* If there is a difference */
159             if (!myDifference.isIdentical()) {
160                 /* Declare the field */
161                 myLocal.declareLocalField(myField.getFieldId(), f -> new MetisDataDelta(myObject, myDifference));
162             }
163         }
164 
165         /* Return the fields */
166         return myLocal;
167     }
168 }