View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.tutorial.property.custom;
17  
18  import java.io.Serializable;
19  
20  import org.apache.commons.lang.ObjectUtils;
21  import org.apache.commons.lang.StringUtils;
22  
23  import de.smartics.util.lang.Arg;
24  
25  /**
26   * Sample to show how to construct instances of custom types.
27   */
28  public final class CredentialsProperty implements Serializable
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    /**
35     * The class version identifier.
36     */
37    private static final long serialVersionUID = 1L;
38  
39    // --- members --------------------------------------------------------------
40  
41    private final String userId;
42  
43    private final String password;
44  
45    // ****************************** Initializer *******************************
46  
47    // ****************************** Constructors ******************************
48  
49    /**
50     * Default constructor.
51     */
52    private CredentialsProperty(final Builder builder)
53    {
54      this.userId = builder.userId;
55      this.password = builder.password;
56    }
57  
58    // ****************************** Inner Classes *****************************
59  
60    private static final class Builder
61    {
62      private String userId;
63  
64      private String password;
65  
66      Builder withUserId(final String userId)
67      {
68        this.userId = userId;
69        return this;
70      }
71  
72      Builder withPassword(final String password)
73      {
74        this.password = password;
75        return this;
76      }
77  
78      CredentialsProperty build()
79      {
80        return new CredentialsProperty(this);
81      }
82    }
83  
84    // ********************************* Methods ********************************
85  
86    // --- init -----------------------------------------------------------------
87  
88    // --- factory --------------------------------------------------------------
89  
90    public static CredentialsProperty fromString(final String string)
91      throws IllegalArgumentException
92    {
93      Arg.checkNotBlank("string", string);
94  
95      final CredentialsProperty.Builder builder =
96          new CredentialsProperty.Builder();
97  
98      final int splitIndex = string.indexOf(':');
99      if (splitIndex == -1 || splitIndex == string.length() - 1)
100     {
101       builder.withUserId(string);
102     }
103     else
104     {
105       final String userId = string.substring(0, splitIndex);
106       final String password = string.substring(splitIndex + 1);
107       builder.withUserId(userId);
108       builder.withPassword(password);
109     }
110 
111     final CredentialsProperty property = builder.build();
112     return property;
113   }
114 
115   // --- get&set --------------------------------------------------------------
116 
117   public String getUserId()
118   {
119     return userId;
120   }
121 
122   public String getPassword()
123   {
124     return password;
125   }
126 
127   // --- business -------------------------------------------------------------
128 
129   // --- object basics --------------------------------------------------------
130 
131   /**
132    * Returns the hash code of the object.
133    *
134    * @return the hash code.
135    */
136   @Override
137   public int hashCode()
138   {
139     return userId.hashCode();
140   }
141 
142   /**
143    * Returns <code>true</code> if the given object is semantically equal to the
144    * given object, <code>false</code> otherwise.
145    *
146    * @param object the instance to compare to.
147    * @return <code>true</code> if the given object is semantically equal to the
148    *         given object, <code>false</code> otherwise.
149    */
150   @Override
151   public boolean equals(final Object object)
152   {
153     if (this == object)
154     {
155       return true;
156     }
157     else if (object == null || getClass() != object.getClass())
158     {
159       return false;
160     }
161 
162     final CredentialsProperty other = (CredentialsProperty) object;
163 
164     return (userId.equals(other.userId) && ObjectUtils.equals(password,
165         other.password));
166   }
167 
168   /**
169    * Returns the string representation of the object.
170    *
171    * @return the string representation of the object.
172    */
173   @Override
174   public String toString()
175   {
176     final StringBuilder buffer = new StringBuilder();
177 
178     buffer.append(userId);
179 
180     if (StringUtils.isNotBlank(password))
181     {
182       buffer.append(':').append(password);
183     }
184 
185     return buffer.toString();
186   }
187 }