View Javadoc

1   /**
2    * Licensed to jclouds, Inc. (jclouds) under one or more
3    * contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  jclouds licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.jclouds.io.payloads;
20  
21  import java.io.Serializable;
22  import java.util.Arrays;
23  
24  import org.jclouds.io.ContentMetadata;
25  import org.jclouds.io.ContentMetadataBuilder;
26  
27  /**
28   * @author Adrian Cole
29   */
30  public class BaseImmutableContentMetadata implements ContentMetadata, Serializable {
31  
32     /** The serialVersionUID */
33     private static final long serialVersionUID = -1445533440795766130L;
34     protected String contentType;
35     protected Long contentLength;
36     protected byte[] contentMD5;
37     protected String contentDisposition;
38     protected String contentLanguage;
39     protected String contentEncoding;
40  
41     public BaseImmutableContentMetadata(String contentType, Long contentLength, byte[] contentMD5,
42              String contentDisposition, String contentLanguage, String contentEncoding) {
43        this.contentType = contentType;
44        this.contentLength = contentLength;
45        this.contentMD5 = contentMD5;
46        this.contentDisposition = contentDisposition;
47        this.contentLanguage = contentLanguage;
48        this.contentEncoding = contentEncoding;
49     }
50  
51     /**
52      * {@inheritDoc}
53      */
54     @Override
55     public Long getContentLength() {
56        return contentLength;
57     }
58  
59     /**
60      * {@inheritDoc}
61      */
62     @Override
63     public byte[] getContentMD5() {
64        if (contentMD5 != null) {
65           byte[] retval = new byte[contentMD5.length];
66           System.arraycopy(this.contentMD5, 0, retval, 0, contentMD5.length);
67           return retval;
68        } else {
69           return null;
70        }
71     }
72  
73     /**
74      * {@inheritDoc}
75      */
76     @Override
77     public String getContentType() {
78        return contentType;
79     }
80  
81     /**
82      * {@inheritDoc}
83      */
84     @Override
85     public String getContentDisposition() {
86        return this.contentDisposition;
87     }
88  
89     /**
90      * {@inheritDoc}
91      */
92     @Override
93     public String getContentLanguage() {
94        return this.contentLanguage;
95     }
96  
97     /**
98      * {@inheritDoc}
99      */
100    @Override
101    public String getContentEncoding() {
102       return this.contentEncoding;
103    }
104 
105    @Override
106    public String toString() {
107       return "[contentType=" + contentType + ", contentLength=" + contentLength + ", contentDisposition="
108                + contentDisposition + ", contentEncoding=" + contentEncoding + ", contentLanguage=" + contentLanguage
109                + ", contentMD5=" + Arrays.toString(contentMD5) + "]";
110    }
111 
112    @Override
113    public int hashCode() {
114       final int prime = 31;
115       int result = 1;
116       result = prime * result + ((contentDisposition == null) ? 0 : contentDisposition.hashCode());
117       result = prime * result + ((contentEncoding == null) ? 0 : contentEncoding.hashCode());
118       result = prime * result + ((contentLanguage == null) ? 0 : contentLanguage.hashCode());
119       result = prime * result + ((contentLength == null) ? 0 : contentLength.hashCode());
120       result = prime * result + Arrays.hashCode(contentMD5);
121       result = prime * result + ((contentType == null) ? 0 : contentType.hashCode());
122       return result;
123    }
124 
125    @Override
126    public boolean equals(Object obj) {
127       if (this == obj)
128          return true;
129       if (obj == null)
130          return false;
131       if (getClass() != obj.getClass())
132          return false;
133       BaseImmutableContentMetadata other = (BaseImmutableContentMetadata) obj;
134       if (contentDisposition == null) {
135          if (other.contentDisposition != null)
136             return false;
137       } else if (!contentDisposition.equals(other.contentDisposition))
138          return false;
139       if (contentEncoding == null) {
140          if (other.contentEncoding != null)
141             return false;
142       } else if (!contentEncoding.equals(other.contentEncoding))
143          return false;
144       if (contentLanguage == null) {
145          if (other.contentLanguage != null)
146             return false;
147       } else if (!contentLanguage.equals(other.contentLanguage))
148          return false;
149       if (contentLength == null) {
150          if (other.contentLength != null)
151             return false;
152       } else if (!contentLength.equals(other.contentLength))
153          return false;
154       if (!Arrays.equals(contentMD5, other.contentMD5))
155          return false;
156       if (contentType == null) {
157          if (other.contentType != null)
158             return false;
159       } else if (!contentType.equals(other.contentType))
160          return false;
161       return true;
162    }
163 
164    @Override
165    public ContentMetadataBuilder toBuilder() {
166       return ContentMetadataBuilder.fromContentMetadata(this);
167    }
168 
169 }