Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / cacheinvalidation / src / java / com / google / ipc / invalidation / external / client / types / ObjectId.java
1 /*
2  * Copyright 2011 Google Inc.
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
17 package com.google.ipc.invalidation.external.client.types;
18
19 import com.google.ipc.invalidation.util.Preconditions;
20
21 import java.util.Arrays;
22
23 /**
24  * A class to represent a unique object id that an application can register or
25  * unregister for.
26  *
27  */
28 public final class ObjectId {
29
30   /** The invalidation source type. */
31   private final int source;
32
33   /** The name/unique id for the object. */
34   private final byte[] name;
35
36   /**
37    * Creates an object id for the given {@code source} and id {@code name} (does not make a copy of
38    * the array).
39    */
40   public static ObjectId newInstance(int source, byte[] name) {
41     return new ObjectId(source, name);
42   }
43
44   /** Creates an object id for the given {@code source} and id {@code name}. */
45   private ObjectId(int source, byte[] name) {
46     Preconditions.checkState(source >= 0, "source");
47     this.source = source;
48     this.name = Preconditions.checkNotNull(name, "name");
49   }
50
51   public int getSource() {
52     return source;
53   }
54
55   public byte[] getName() {
56     return name;
57   }
58
59   @Override
60   public boolean equals(Object object) {
61     if (object == this) {
62       return true;
63     }
64
65     if (!(object instanceof ObjectId)) {
66       return false;
67     }
68
69     final ObjectId other = (ObjectId) object;
70     if ((source != other.source) || !Arrays.equals(name, other.name)) {
71       return false;
72     }
73     return true;
74   }
75
76   @Override
77   public int hashCode() {
78     return source ^ Arrays.hashCode(name);
79   }
80
81   @Override
82   public String toString() {
83     return "Oid: <" + source + ", " + BytesFormatter.toString(name) + ">";
84   }
85 }