EMMA Coverage Report (generated Wed Oct 26 13:47:17 EDT 2011)
[all classes][org.jclouds.io]

COVERAGE SUMMARY FOR SOURCE FILE [ContentMetadataBuilder.java]

nameclass, %method, %block, %line, %
ContentMetadataBuilder.java100% (2/2)69%  (11/16)42%  (186/444)37%  (30.1/81)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ContentMetadataBuilder100% (1/1)64%  (9/14)39%  (164/422)36%  (29.1/80)
build (): ContentMetadata 0%   (0/1)0%   (0/16)0%   (0/1)
create (): ContentMetadataBuilder 0%   (0/1)0%   (0/4)0%   (0/1)
equals (Object): boolean 0%   (0/1)0%   (0/109)0%   (0/35)
fromContentMetadata (ContentMetadata): ContentMetadataBuilder 0%   (0/1)0%   (0/22)0%   (0/1)
hashCode (): int 0%   (0/1)0%   (0/79)0%   (0/9)
fromHttpHeaders (Multimap): ContentMetadataBuilder 100% (1/1)73%  (77/105)74%  (11.1/15)
ContentMetadataBuilder (): void 100% (1/1)100% (6/6)100% (2/2)
contentDisposition (String): ContentMetadataBuilder 100% (1/1)100% (5/5)100% (2/2)
contentEncoding (String): ContentMetadataBuilder 100% (1/1)100% (5/5)100% (2/2)
contentLanguage (String): ContentMetadataBuilder 100% (1/1)100% (5/5)100% (2/2)
contentLength (Long): ContentMetadataBuilder 100% (1/1)100% (5/5)100% (2/2)
contentMD5 (byte []): ContentMetadataBuilder 100% (1/1)100% (18/18)100% (5/5)
contentType (String): ContentMetadataBuilder 100% (1/1)100% (5/5)100% (2/2)
toString (): String 100% (1/1)100% (38/38)100% (1/1)
     
class ContentMetadataBuilder$1100% (1/1)100% (2/2)100% (22/22)100% (2/2)
ContentMetadataBuilder$1 (ContentMetadataBuilder): void 100% (1/1)100% (6/6)100% (1/1)
apply (Map$Entry): boolean 100% (1/1)100% (16/16)100% (1/1)

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 */
19package org.jclouds.io;
20 
21import static com.google.common.collect.Iterables.any;
22import static javax.ws.rs.core.HttpHeaders.CONTENT_LENGTH;
23import static javax.ws.rs.core.HttpHeaders.CONTENT_TYPE;
24 
25import java.io.Serializable;
26import java.util.Arrays;
27import java.util.Map.Entry;
28 
29import org.jclouds.javax.annotation.Nullable;
30 
31import org.jclouds.crypto.CryptoStreams;
32import org.jclouds.io.payloads.BaseImmutableContentMetadata;
33 
34import com.google.common.base.Predicate;
35import com.google.common.collect.Multimap;
36 
37/**
38 * @author Adrian Cole
39 */
40public class ContentMetadataBuilder implements Serializable {
41   /** The serialVersionUID */
42   private static final long serialVersionUID = -5279643002875371558L;
43 
44   public static ContentMetadataBuilder create() {
45      return new ContentMetadataBuilder();
46   }
47 
48   protected String contentType = "application/unknown";
49   protected Long contentLength;
50   protected byte[] contentMD5;
51   protected String contentDisposition;
52   protected String contentLanguage;
53   protected String contentEncoding;
54 
55   public ContentMetadataBuilder fromHttpHeaders(Multimap<String, String> headers) {
56      boolean chunked = any(headers.entries(), new Predicate<Entry<String, String>>() {
57         @Override
58         public boolean apply(Entry<String, String> input) {
59            return "Transfer-Encoding".equalsIgnoreCase(input.getKey()) && "chunked".equalsIgnoreCase(input.getValue());
60         }
61      });
62      for (Entry<String, String> header : headers.entries()) {
63         if (!chunked && CONTENT_LENGTH.equalsIgnoreCase(header.getKey())) {
64            contentLength(new Long(header.getValue()));
65         } else if ("Content-MD5".equalsIgnoreCase(header.getKey())) {
66            contentMD5(CryptoStreams.base64(header.getValue()));
67         } else if (CONTENT_TYPE.equalsIgnoreCase(header.getKey())) {
68            contentType(header.getValue());
69         } else if ("Content-Disposition".equalsIgnoreCase(header.getKey())) {
70            contentDisposition(header.getValue());
71         } else if ("Content-Encoding".equalsIgnoreCase(header.getKey())) {
72            contentEncoding(header.getValue());
73         } else if ("Content-Language".equalsIgnoreCase(header.getKey())) {
74            contentLanguage(header.getValue());
75         }
76      }
77      return this;
78   }
79 
80   public ContentMetadataBuilder contentLength(@Nullable Long contentLength) {
81      this.contentLength = contentLength;
82      return this;
83   }
84 
85   public ContentMetadataBuilder contentMD5(byte[] md5) {
86      if (md5 != null) {
87         byte[] retval = new byte[md5.length];
88         System.arraycopy(md5, 0, retval, 0, md5.length);
89         this.contentMD5 = md5;
90      }
91      return this;
92 
93   }
94 
95   public ContentMetadataBuilder contentType(@Nullable String contentType) {
96      this.contentType = contentType;
97      return this;
98   }
99 
100   public ContentMetadataBuilder contentDisposition(@Nullable String contentDisposition) {
101      this.contentDisposition = contentDisposition;
102      return this;
103 
104   }
105 
106   public ContentMetadataBuilder contentLanguage(@Nullable String contentLanguage) {
107      this.contentLanguage = contentLanguage;
108      return this;
109   }
110 
111   public ContentMetadataBuilder contentEncoding(@Nullable String contentEncoding) {
112      this.contentEncoding = contentEncoding;
113      return this;
114   }
115 
116   public ContentMetadata build() {
117      return new BaseImmutableContentMetadata(contentType, contentLength, contentMD5, contentDisposition,
118               contentLanguage, contentEncoding);
119   }
120 
121   public static ContentMetadataBuilder fromContentMetadata(ContentMetadata in) {
122      return new ContentMetadataBuilder().contentType(in.getContentType()).contentLength(in.getContentLength())
123               .contentMD5(in.getContentMD5()).contentDisposition(in.getContentDisposition()).contentLanguage(
124                        in.getContentLanguage()).contentEncoding(in.getContentEncoding());
125   }
126 
127   @Override
128   public int hashCode() {
129      final int prime = 31;
130      int result = 1;
131      result = prime * result + ((contentDisposition == null) ? 0 : contentDisposition.hashCode());
132      result = prime * result + ((contentEncoding == null) ? 0 : contentEncoding.hashCode());
133      result = prime * result + ((contentLanguage == null) ? 0 : contentLanguage.hashCode());
134      result = prime * result + ((contentLength == null) ? 0 : contentLength.hashCode());
135      result = prime * result + Arrays.hashCode(contentMD5);
136      result = prime * result + ((contentType == null) ? 0 : contentType.hashCode());
137      return result;
138   }
139 
140   @Override
141   public boolean equals(Object obj) {
142      if (this == obj)
143         return true;
144      if (obj == null)
145         return false;
146      if (getClass() != obj.getClass())
147         return false;
148      ContentMetadataBuilder other = (ContentMetadataBuilder) obj;
149      if (contentDisposition == null) {
150         if (other.contentDisposition != null)
151            return false;
152      } else if (!contentDisposition.equals(other.contentDisposition))
153         return false;
154      if (contentEncoding == null) {
155         if (other.contentEncoding != null)
156            return false;
157      } else if (!contentEncoding.equals(other.contentEncoding))
158         return false;
159      if (contentLanguage == null) {
160         if (other.contentLanguage != null)
161            return false;
162      } else if (!contentLanguage.equals(other.contentLanguage))
163         return false;
164      if (contentLength == null) {
165         if (other.contentLength != null)
166            return false;
167      } else if (!contentLength.equals(other.contentLength))
168         return false;
169      if (!Arrays.equals(contentMD5, other.contentMD5))
170         return false;
171      if (contentType == null) {
172         if (other.contentType != null)
173            return false;
174      } else if (!contentType.equals(other.contentType))
175         return false;
176      return true;
177   }
178 
179   @Override
180   public String toString() {
181      return "[contentDisposition=" + contentDisposition + ", contentEncoding=" + contentEncoding
182               + ", contentLanguage=" + contentLanguage + ", contentLength=" + contentLength + ", contentMD5="
183               + Arrays.toString(contentMD5) + ", contentType=" + contentType + "]";
184   }
185}

[all classes][org.jclouds.io]
EMMA 2.0.5312 (C) Vladimir Roubtsov