Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Id.java
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.common;
16 
17 import java.util.ArrayList;
18 import java.util.List;
19 
20 import com.google.common.base.Joiner;
21 import com.google.common.collect.Lists;
22 
26 public class Id<IdType extends Id<IdType>> implements Comparable<Id<IdType>> {
27  static protected int INVALID_ID = -1;
28  protected final int id_;
29 
30  public Id(int id) {
31  this.id_ = id;
32  }
33 
34  public boolean isValid() { return id_ != INVALID_ID; }
35  public int asInt() { return id_; }
36 
37  @Override
38  public int hashCode() {
39  return Integer.valueOf(id_).hashCode();
40  }
41 
42  @Override
43  public String toString() {
44  return Integer.toString(id_);
45  }
46 
47  @Override
48  public boolean equals(Object obj) {
49  if (obj == null) return false;
50  // only ids of the same subclass are comparable
51  if (obj.getClass() != this.getClass()) return false;
52  return ((Id)obj).id_ == id_;
53  }
54 
55  @Override
56  public int compareTo(Id<IdType> cmp) {
57  return id_ - cmp.id_;
58  }
59 
60  public ArrayList<IdType> asList() {
61  ArrayList<IdType> list = new ArrayList<IdType>();
62  list.add((IdType) this);
63  return list;
64  }
65 
66  public static <C extends Id> String printIds(List<C> ids) {
67  ArrayList<String> l = Lists.newArrayList();
68  for (C id: ids) {
69  l.add(id.toString());
70  }
71  return "(" + Joiner.on(" ").join(l) + ")";
72  }
73 }
static< CextendsId > String printIds(List< C > ids)
Definition: Id.java:66