View Javadoc

1   /*
2    * Copyright 2010 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.fatwire.gst.foundation.controller;
17  
18  import java.io.Serializable;
19  
20  import com.fatwire.assetapi.data.AssetId;
21  import com.openmarket.xcelerate.asset.AssetIdImpl;
22  
23  /**
24   * AssetId with SiteName attached to it
25   * 
26   * @author Dolf Dijkstra
27   * @since Jun 17, 2010
28   */
29  public class AssetIdWithSite implements AssetId, Comparable<AssetIdWithSite>, Serializable {
30  
31      private static final long serialVersionUID = 1L;
32      private final AssetIdImpl id;
33      private final String site;
34  
35      public AssetIdWithSite(AssetId id, String site) {
36          if (id == null)
37              throw new NullPointerException("id can not be null.");
38          if (id instanceof AssetIdImpl) {
39              this.id = (AssetIdImpl) id;
40          } else {
41              this.id = new AssetIdImpl(id.getType(), id.getId());
42          }
43          this.site = site;
44      }
45  
46      public AssetIdWithSite(String type, long id, String site) {
47          if (type == null || site == null)
48              throw new NullPointerException("type=" + type + ",site=" + site);
49          this.id = new AssetIdImpl(type, id);
50          this.site = site;
51  
52      }
53  
54      /*
55       * (non-Javadoc)
56       * 
57       * @see com.fatwire.assetapi.data.AssetId#getId()
58       */
59  
60      public long getId() {
61          return id.getId();
62      }
63  
64      /*
65       * (non-Javadoc)
66       * 
67       * @see com.fatwire.assetapi.data.AssetId#getType()
68       */
69  
70      public String getType() {
71          return id.getType();
72      }
73  
74      public AssetId getAssetId() {
75          return id;
76      }
77  
78      /**
79       * @return the site
80       */
81      public String getSite() {
82          return site;
83      }
84  
85      /*
86       * (non-Javadoc)
87       * 
88       * @see java.lang.Object#toString()
89       */
90  
91      @Override
92      public String toString() {
93          return "AssetIdWithSite [type=" + id.getType() + ", id=" + id.getId() + ", site=" + site + "]";
94      }
95  
96      /*
97       * (non-Javadoc)
98       * 
99       * @see java.lang.Comparable#compareTo(java.lang.Object)
100      */
101 
102     public int compareTo(AssetIdWithSite o) {
103         if (this == o)
104             return 0;
105         int l = id.compareTo(o.getAssetId());
106         if (l != 0)
107             return l;
108 
109         return this.site.compareTo(o.site);
110     }
111 
112     /*
113      * (non-Javadoc)
114      * 
115      * @see java.lang.Object#hashCode()
116      */
117     @Override
118     public int hashCode() {
119         final int prime = 31;
120         int result = 1;
121         result = prime * result + ((id == null) ? 0 : id.hashCode());
122         result = prime * result + ((site == null) ? 0 : site.hashCode());
123         return result;
124     }
125 
126     /*
127      * (non-Javadoc)
128      * 
129      * @see java.lang.Object#equals(java.lang.Object)
130      */
131     @Override
132     public boolean equals(Object obj) {
133         if (this == obj)
134             return true;
135         if (obj == null)
136             return false;
137         if (obj instanceof AssetIdImpl)
138             return id.equals(obj);
139         if (!(obj instanceof AssetIdWithSite))
140             return false;
141         AssetIdWithSite other = (AssetIdWithSite) obj;
142         if (id == null) {
143             if (other.id != null)
144                 return false;
145         } else if (!id.equals(other.id))
146             return false;
147         if (site == null) {
148             if (other.site != null)
149                 return false;
150         } else if (!site.equals(other.site))
151             return false;
152         return true;
153     }
154 
155 }