View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.model.monaco;
23  
24  import java.io.Serializable;
25  
26  import org.primefaces.shaded.owasp.encoder.Encode;
27  
28  /**
29   * Immutable Model for the Monaco diff code editor that encapsulates two strings, corresponding to the value of the original and modified editor.
30   * 
31   * @since 11.1.0
32   */
33  public class MonacoDiffEditorModel implements Serializable {
34      private static final long serialVersionUID = 1L;
35  
36      private final static MonacoDiffEditorModel EMPTY = new MonacoDiffEditorModel();
37  
38      private final String originalValue;
39  
40      private final String modifiedValue;
41  
42      public MonacoDiffEditorModel() {
43          this.originalValue = "";
44          this.modifiedValue = "";
45      }
46  
47      public MonacoDiffEditorModel(String originalValue, String modifiedValue) {
48          this.originalValue = originalValue != null ? originalValue : "";
49          this.modifiedValue = modifiedValue != null ? modifiedValue : "";
50      }
51  
52      public MonacoDiffEditorModel withOriginal(String originalValue) {
53          return new MonacoDiffEditorModel(originalValue, modifiedValue);
54      }
55  
56      public MonacoDiffEditorModel withModified(String modifiedValue) {
57          return new MonacoDiffEditorModel(originalValue, modifiedValue);
58      }
59  
60      /**
61       * @return The original text against which perform the comparison.
62       */
63      public String getOriginalValue() {
64          return originalValue;
65      }
66  
67      /**
68       * @return The modified text to compare against the original text.
69       */
70      public String getModifiedValue() {
71          return modifiedValue;
72      }
73  
74      @Override
75      public int hashCode() {
76          final int prime = 31;
77          int result = 1;
78          result = prime * result + ((modifiedValue == null) ? 0 : modifiedValue.hashCode());
79          result = prime * result + ((originalValue == null) ? 0 : originalValue.hashCode());
80          return result;
81      }
82  
83      @Override
84      public boolean equals(Object obj) {
85          if (this == obj) {
86              return true;
87          }
88          if (obj == null) {
89              return false;
90          }
91          if (getClass() != obj.getClass()) {
92              return false;
93          }
94          MonacoDiffEditorModel other = (MonacoDiffEditorModel) obj;
95          if (modifiedValue == null) {
96              if (other.modifiedValue != null) {
97                  return false;
98              }
99          }
100         else if (!modifiedValue.equals(other.modifiedValue)) {
101             return false;
102         }
103         if (originalValue == null) {
104             if (other.originalValue != null) {
105                 return false;
106             }
107         }
108         else if (!originalValue.equals(other.originalValue)) {
109             return false;
110         }
111         return true;
112     }
113 
114     /**
115      * @return Whether this model is empty, e.g. both the original and modified values are empty.
116      */
117     public boolean isEmpty() {
118         return originalValue != null && !originalValue.isEmpty() && modifiedValue != null && !modifiedValue.isEmpty();
119     }
120 
121     @Override
122     public String toString() {
123         return "[\"" + Encode.forJava(originalValue) + "\",\"" + Encode.forJava(modifiedValue) + "\"]";
124     }
125 
126     /**
127      * @return An empty model with no value.
128      */
129     public static MonacoDiffEditorModel empty() {
130         return EMPTY;
131     }
132 }