| 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.atmos.domain.internal; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import javax.inject.Inject; |
| 24 | import javax.inject.Provider; |
| 25 | import javax.inject.Singleton; |
| 26 | |
| 27 | import org.jclouds.atmos.domain.AtmosObject; |
| 28 | import org.jclouds.atmos.domain.MutableContentMetadata; |
| 29 | import org.jclouds.atmos.domain.SystemMetadata; |
| 30 | import org.jclouds.atmos.domain.UserMetadata; |
| 31 | import org.jclouds.http.internal.PayloadEnclosingImpl; |
| 32 | import org.jclouds.io.Payload; |
| 33 | |
| 34 | import com.google.common.collect.LinkedHashMultimap; |
| 35 | import com.google.common.collect.Multimap; |
| 36 | |
| 37 | /** |
| 38 | * Default Implementation of {@link AtmosObject}. |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | public class AtmosObjectImpl extends PayloadEnclosingImpl implements AtmosObject, Comparable<AtmosObject> { |
| 43 | @Singleton |
| 44 | public static class AtmosObjectFactory implements AtmosObject.Factory { |
| 45 | |
| 46 | @Inject |
| 47 | Provider<MutableContentMetadata> metadataProvider; |
| 48 | |
| 49 | public AtmosObject create(MutableContentMetadata contentMetadata) { |
| 50 | return new AtmosObjectImpl(contentMetadata != null ? contentMetadata : metadataProvider.get()); |
| 51 | } |
| 52 | |
| 53 | public AtmosObject create(SystemMetadata systemMetadata, UserMetadata userMetadata) { |
| 54 | return new AtmosObjectImpl(metadataProvider.get(), systemMetadata, userMetadata); |
| 55 | } |
| 56 | |
| 57 | public AtmosObject create(MutableContentMetadata contentMetadata, SystemMetadata systemMetadata, |
| 58 | UserMetadata userMetadata) { |
| 59 | return new AtmosObjectImpl(contentMetadata, systemMetadata, userMetadata); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private final UserMetadata userMetadata; |
| 64 | private final SystemMetadata systemMetadata; |
| 65 | |
| 66 | public SystemMetadata getSystemMetadata() { |
| 67 | return systemMetadata; |
| 68 | } |
| 69 | |
| 70 | public UserMetadata getUserMetadata() { |
| 71 | return userMetadata; |
| 72 | } |
| 73 | |
| 74 | private MutableContentMetadata contentMetadata; |
| 75 | private Multimap<String, String> allHeaders = LinkedHashMultimap.create(); |
| 76 | |
| 77 | public AtmosObjectImpl(MutableContentMetadata contentMetadata, SystemMetadata systemMetadata, |
| 78 | UserMetadata userMetadata) { |
| 79 | this.contentMetadata = contentMetadata; |
| 80 | this.systemMetadata = systemMetadata; |
| 81 | this.userMetadata = userMetadata; |
| 82 | } |
| 83 | |
| 84 | @Inject |
| 85 | public AtmosObjectImpl(MutableContentMetadata contentMetadata) { |
| 86 | this(contentMetadata, null, new UserMetadata()); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * {@inheritDoc} |
| 91 | */ |
| 92 | @Override |
| 93 | public MutableContentMetadata getContentMetadata() { |
| 94 | return contentMetadata; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * {@inheritDoc} |
| 99 | */ |
| 100 | @Override |
| 101 | public Multimap<String, String> getAllHeaders() { |
| 102 | return allHeaders; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * {@inheritDoc} |
| 107 | */ |
| 108 | @Override |
| 109 | public void setAllHeaders(Multimap<String, String> allHeaders) { |
| 110 | this.allHeaders = checkNotNull(allHeaders, "allHeaders"); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * {@inheritDoc} |
| 115 | */ |
| 116 | @Override |
| 117 | public int compareTo(AtmosObject o) { |
| 118 | if (getContentMetadata().getName() == null) |
| 119 | return -1; |
| 120 | return (this == o) ? 0 : getContentMetadata().getName().compareTo(o.getContentMetadata().getName()); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public int hashCode() { |
| 125 | final int prime = 31; |
| 126 | int result = super.hashCode(); |
| 127 | result = prime * result + ((contentMetadata == null) ? 0 : contentMetadata.hashCode()); |
| 128 | result = prime * result + ((systemMetadata == null) ? 0 : systemMetadata.hashCode()); |
| 129 | result = prime * result + ((userMetadata == null) ? 0 : userMetadata.hashCode()); |
| 130 | return result; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public boolean equals(Object obj) { |
| 135 | if (this == obj) |
| 136 | return true; |
| 137 | if (!super.equals(obj)) |
| 138 | return false; |
| 139 | if (getClass() != obj.getClass()) |
| 140 | return false; |
| 141 | AtmosObjectImpl other = (AtmosObjectImpl) obj; |
| 142 | if (contentMetadata == null) { |
| 143 | if (other.contentMetadata != null) |
| 144 | return false; |
| 145 | } else if (!contentMetadata.equals(other.contentMetadata)) |
| 146 | return false; |
| 147 | if (systemMetadata == null) { |
| 148 | if (other.systemMetadata != null) |
| 149 | return false; |
| 150 | } else if (!systemMetadata.equals(other.systemMetadata)) |
| 151 | return false; |
| 152 | if (userMetadata == null) { |
| 153 | if (other.userMetadata != null) |
| 154 | return false; |
| 155 | } else if (!userMetadata.equals(other.userMetadata)) |
| 156 | return false; |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public String toString() { |
| 162 | return "[contentMetadata=" + contentMetadata + "]"; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void setPayload(Payload data) { |
| 167 | this.payload = data; |
| 168 | this.contentMetadata = new DelegatingMutableContentMetadata(contentMetadata.getUri(), contentMetadata.getName(), |
| 169 | contentMetadata.getPath(), payload.getContentMetadata()); |
| 170 | } |
| 171 | } |