1 | /** |
2 | * |
3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
4 | * |
5 | * ==================================================================== |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * 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, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * ==================================================================== |
18 | */ |
19 | package org.jclouds.openstack.swift.domain.internal; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import javax.inject.Inject; |
24 | |
25 | import org.jclouds.http.HttpUtils; |
26 | import org.jclouds.http.internal.PayloadEnclosingImpl; |
27 | import org.jclouds.io.Payload; |
28 | import org.jclouds.openstack.swift.domain.MutableObjectInfoWithMetadata; |
29 | import org.jclouds.openstack.swift.domain.SwiftObject; |
30 | |
31 | import com.google.common.collect.LinkedHashMultimap; |
32 | import com.google.common.collect.Multimap; |
33 | |
34 | /** |
35 | * Default Implementation of {@link SwiftObject}. |
36 | * |
37 | * @author Adrian Cole |
38 | */ |
39 | public class SwiftObjectImpl extends PayloadEnclosingImpl implements SwiftObject, Comparable<SwiftObject> { |
40 | |
41 | private final DelegatingMutableObjectInfoWithMetadata info; |
42 | private Multimap<String, String> allHeaders = LinkedHashMultimap.create(); |
43 | |
44 | @Inject |
45 | public SwiftObjectImpl(MutableObjectInfoWithMetadata info) { |
46 | super(); |
47 | this.info = new DelegatingMutableObjectInfoWithMetadata(info); |
48 | } |
49 | |
50 | /** |
51 | * {@inheritDoc} |
52 | */ |
53 | @Override |
54 | public MutableObjectInfoWithMetadata getInfo() { |
55 | return info; |
56 | } |
57 | |
58 | /** |
59 | * {@inheritDoc} |
60 | */ |
61 | @Override |
62 | public Multimap<String, String> getAllHeaders() { |
63 | return allHeaders; |
64 | } |
65 | |
66 | /** |
67 | * {@inheritDoc} |
68 | */ |
69 | @Override |
70 | public void setAllHeaders(Multimap<String, String> allHeaders) { |
71 | this.allHeaders = checkNotNull(allHeaders, "allHeaders"); |
72 | } |
73 | |
74 | /** |
75 | * {@inheritDoc} |
76 | */ |
77 | @Override |
78 | public int compareTo(SwiftObject o) { |
79 | if (getInfo().getName() == null) |
80 | return -1; |
81 | return (this == o) ? 0 : getInfo().getName().compareTo(o.getInfo().getName()); |
82 | } |
83 | |
84 | @Override |
85 | public int hashCode() { |
86 | final int prime = 31; |
87 | int result = super.hashCode(); |
88 | result = prime * result + ((info == null) ? 0 : info.hashCode()); |
89 | return result; |
90 | } |
91 | |
92 | @Override |
93 | public boolean equals(Object obj) { |
94 | if (this == obj) |
95 | return true; |
96 | if (!super.equals(obj)) |
97 | return false; |
98 | if (getClass() != obj.getClass()) |
99 | return false; |
100 | SwiftObjectImpl other = (SwiftObjectImpl) obj; |
101 | if (info == null) { |
102 | if (other.info != null) |
103 | return false; |
104 | } else if (!info.equals(other.info)) |
105 | return false; |
106 | return true; |
107 | } |
108 | |
109 | @Override |
110 | public String toString() { |
111 | return "[info=" + info + "]"; |
112 | } |
113 | |
114 | @Override |
115 | public void setPayload(Payload data) { |
116 | HttpUtils.copy(data.getContentMetadata(), info); |
117 | data.setContentMetadata(info); |
118 | super.setPayload(data); |
119 | } |
120 | |
121 | } |