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.http.internal; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | import static org.jclouds.io.Payloads.newPayload; |
23 | |
24 | import java.io.File; |
25 | import java.io.InputStream; |
26 | |
27 | import org.jclouds.javax.annotation.Nullable; |
28 | |
29 | import org.jclouds.io.Payload; |
30 | import org.jclouds.io.PayloadEnclosing; |
31 | |
32 | /** |
33 | * |
34 | * @author Adrian Cole |
35 | */ |
36 | public class PayloadEnclosingImpl implements PayloadEnclosing { |
37 | protected Payload payload; |
38 | |
39 | public PayloadEnclosingImpl() { |
40 | this(null); |
41 | } |
42 | |
43 | public PayloadEnclosingImpl(@Nullable Payload payload) { |
44 | this.payload = payload; |
45 | } |
46 | |
47 | /** |
48 | * {@inheritDoc} |
49 | */ |
50 | @Override |
51 | public Payload getPayload() { |
52 | return payload; |
53 | } |
54 | |
55 | /** |
56 | * {@inheritDoc} |
57 | */ |
58 | @Override |
59 | public void setPayload(Payload data) { |
60 | if (this.payload != null) |
61 | payload.release(); |
62 | this.payload = checkNotNull(data, "data"); |
63 | } |
64 | |
65 | /** |
66 | * {@inheritDoc} |
67 | */ |
68 | @Override |
69 | public void setPayload(InputStream data) { |
70 | setPayload(newPayload(checkNotNull(data, "data"))); |
71 | } |
72 | |
73 | /** |
74 | * {@inheritDoc} |
75 | */ |
76 | @Override |
77 | public void setPayload(byte[] data) { |
78 | setPayload(newPayload(checkNotNull(data, "data"))); |
79 | } |
80 | |
81 | /** |
82 | * {@inheritDoc} |
83 | */ |
84 | @Override |
85 | public void setPayload(String data) { |
86 | setPayload(newPayload(checkNotNull(data, "data"))); |
87 | } |
88 | |
89 | /** |
90 | * {@inheritDoc} |
91 | */ |
92 | @Override |
93 | public void setPayload(File data) { |
94 | setPayload(newPayload(checkNotNull(data, "data"))); |
95 | } |
96 | |
97 | @Override |
98 | public int hashCode() { |
99 | final int prime = 31; |
100 | int result = 1; |
101 | result = prime * result + ((payload == null) ? 0 : payload.hashCode()); |
102 | return result; |
103 | } |
104 | |
105 | @Override |
106 | public boolean equals(Object obj) { |
107 | if (this == obj) |
108 | return true; |
109 | if (obj == null) |
110 | return false; |
111 | if (getClass() != obj.getClass()) |
112 | return false; |
113 | PayloadEnclosingImpl other = (PayloadEnclosingImpl) obj; |
114 | if (payload == null) { |
115 | if (other.payload != null) |
116 | return false; |
117 | } else if (!payload.equals(other.payload)) |
118 | return false; |
119 | return true; |
120 | } |
121 | |
122 | } |