View Javadoc

1   /*
2    * Copyright (c) 2009-2012 The 99 Software Foundation
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.nnsoft.sameas4j.cache;
23  
24  import static java.lang.String.format;
25  
26  import java.io.Serializable;
27  
28  /**
29   * The {@link Cache} key representation.
30   */
31  public final class CacheKey implements Serializable {
32  
33      /**
34       * The serialVersionUID constant.
35       */
36      private static final long serialVersionUID = 634054729365372230L;
37  
38      /**
39       * The sameas service.
40       */
41      private final String service;
42  
43      /**
44       * The sameas service "Last-modified" header.
45       */
46      private final long lastModified;
47  
48      /**
49       * Creates a new {@code Cache} key instance.
50       *
51       * @param service The sameas service
52       * @param lastModified The sameas service "Last-modified" header
53       */
54      public CacheKey(String service, long lastModified) {
55          if (service == null) {
56              throw new IllegalArgumentException("Parameter 'service' must not be null");
57          }
58  
59          this.service = service;
60          this.lastModified = lastModified;
61      }
62  
63      /**
64       * Returns the sameas service.
65       *
66       * @return The sameas service
67       */
68      public String getService() {
69          return this.service;
70      }
71  
72      /**
73       * Returns the sameas service "Last-modified" header.
74       *
75       * @return The sameas service "Last-modified" header
76       */
77      public long getLastModified() {
78          return this.lastModified;
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public int hashCode() {
86          final int prime = 31;
87          int result = 1;
88          result = prime * result + (int) (lastModified ^ (lastModified >>> 32));
89          result = prime * result + this.service.hashCode();
90          return result;
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96       @Override
97      public boolean equals(Object obj) {
98          if (this == obj) {
99              return true;
100         }
101 
102         if (obj == null) {
103             return false;
104         }
105 
106         if (getClass() != obj.getClass()) {
107             return false;
108         }
109 
110         CacheKey other = (CacheKey) obj;
111         if (this.lastModified != other.getLastModified()) {
112             return false;
113         }
114         if (this.service == null) {
115             if (other.getService() != null) {
116                 return false;
117             }
118         } else if (!this.service.equals(other.getService())) {
119             return false;
120         }
121 
122         return true;
123     }
124 
125      /**
126       * {@inheritDoc}
127       */
128      @Override
129     public String toString() {
130         return format("CacheKey (service=%s, lastModified=%s)", service, lastModified);
131     }
132 
133 }