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.fluidgrid;
23  
24  import java.io.Serializable;
25  import java.util.Objects;
26  import java.util.UUID;
27  
28  import org.primefaces.extensions.model.common.KeyData;
29  
30  /**
31   * Class representing an item inside of <code>FluidGrid</code>.
32   *
33   * @author Oleg Varaksin / last modified by Melloware
34   * @since 1.1.0
35   */
36  public class FluidGridItem implements KeyData, Serializable {
37  
38      public static final String DEFAULT_TYPE = "default";
39      private static final long serialVersionUID = 1L;
40  
41      private String key;
42      private Serializable data;
43      private String type;
44  
45      public FluidGridItem() {
46          // generate key
47          setKey(generateKey());
48      }
49  
50      public FluidGridItem(final Serializable data) {
51          this();
52          this.data = data;
53          type = DEFAULT_TYPE;
54      }
55  
56      public FluidGridItem(final Serializable data, final String type) {
57          this.data = data;
58          if (type != null) {
59              this.type = type;
60          }
61          else {
62              this.type = DEFAULT_TYPE;
63          }
64  
65          // generate key
66          setKey(generateKey());
67      }
68  
69      @Override
70      public String getKey() {
71          return key;
72      }
73  
74      @Override
75      public void setKey(final String key) {
76          this.key = key;
77      }
78  
79      @Override
80      public Serializable getData() {
81          return data;
82      }
83  
84      @Override
85      public void setData(final Serializable data) {
86          this.data = data;
87      }
88  
89      public String getType() {
90          return type;
91      }
92  
93      public static String generateKey() {
94          return UUID.randomUUID().toString().replace("-", "").substring(0, 8);
95      }
96  
97      @Override
98      public boolean equals(Object o) {
99          if (this == o) {
100             return true;
101         }
102         if (!(o instanceof FluidGridItem)) {
103             return false;
104         }
105         FluidGridItem that = (FluidGridItem) o;
106         return Objects.equals(getKey(), that.getKey());
107     }
108 
109     @Override
110     public int hashCode() {
111         return Objects.hash(getKey());
112     }
113 
114     @Override
115     public String toString() {
116         final StringBuilder builder = new StringBuilder();
117         builder.append("FluidGridItem [key=");
118         builder.append(key);
119         builder.append(", data=");
120         builder.append(data);
121         builder.append(", type=");
122         builder.append(type);
123         builder.append("]");
124         return builder.toString();
125     }
126 
127 }