| 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.blobstore.domain.internal; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import javax.inject.Inject; |
| 24 | |
| 25 | import org.jclouds.blobstore.domain.Blob; |
| 26 | import org.jclouds.blobstore.domain.MutableBlobMetadata; |
| 27 | import org.jclouds.blobstore.domain.StorageMetadata; |
| 28 | import org.jclouds.http.internal.PayloadEnclosingImpl; |
| 29 | import org.jclouds.io.Payload; |
| 30 | |
| 31 | import com.google.common.collect.LinkedHashMultimap; |
| 32 | import com.google.common.collect.Multimap; |
| 33 | |
| 34 | /** |
| 35 | * Value type for an HTTP Blob service. Blobs are stored in {@link StorageMetadata containers} and consist |
| 36 | * of a {@link org.jclouds.blobstore.domain.Value#getInput() value}, a {@link Blob#getKey key and |
| 37 | * |
| 38 | * @link Blob.Metadata#getUserMetadata() metadata} |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | public class BlobImpl extends PayloadEnclosingImpl implements Blob, Comparable<Blob> { |
| 43 | |
| 44 | private final MutableBlobMetadata metadata; |
| 45 | private Multimap<String, String> allHeaders = LinkedHashMultimap.create(); |
| 46 | |
| 47 | @Inject |
| 48 | public BlobImpl(MutableBlobMetadata metadata) { |
| 49 | super(); |
| 50 | this.metadata = metadata; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * {@inheritDoc} |
| 55 | */ |
| 56 | @Override |
| 57 | public MutableBlobMetadata getMetadata() { |
| 58 | return metadata; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * {@inheritDoc} |
| 63 | */ |
| 64 | @Override |
| 65 | public Multimap<String, String> getAllHeaders() { |
| 66 | return allHeaders; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * {@inheritDoc} |
| 71 | */ |
| 72 | @Override |
| 73 | public void setAllHeaders(Multimap<String, String> allHeaders) { |
| 74 | this.allHeaders = checkNotNull(allHeaders, "allHeaders"); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * {@inheritDoc} |
| 79 | */ |
| 80 | @Override |
| 81 | public int compareTo(Blob o) { |
| 82 | if (getMetadata().getName() == null) |
| 83 | return -1; |
| 84 | return (this == o) ? 0 : getMetadata().getName().compareTo(o.getMetadata().getName()); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int hashCode() { |
| 89 | final int prime = 31; |
| 90 | int result = super.hashCode(); |
| 91 | result = prime * result + ((metadata == null) ? 0 : metadata.hashCode()); |
| 92 | return result; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean equals(Object obj) { |
| 97 | if (this == obj) |
| 98 | return true; |
| 99 | if (!super.equals(obj)) |
| 100 | return false; |
| 101 | if (getClass() != obj.getClass()) |
| 102 | return false; |
| 103 | BlobImpl other = (BlobImpl) obj; |
| 104 | if (metadata == null) { |
| 105 | if (other.metadata != null) |
| 106 | return false; |
| 107 | } else if (!metadata.equals(other.metadata)) |
| 108 | return false; |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public String toString() { |
| 114 | return "[metadata=" + metadata + "]"; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void setPayload(Payload data) { |
| 119 | super.setPayload(data); |
| 120 | metadata.setContentMetadata(data.getContentMetadata()); |
| 121 | } |
| 122 | |
| 123 | } |