Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ListMap.java
Go to the documentation of this file.
1 // Copyright 2014 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.util;
16 
17 import java.util.Map;
18 import java.util.ArrayList;
19 import java.util.List;
20 
21 import com.google.common.base.Preconditions;
22 import com.google.common.collect.Maps;
23 import com.google.common.collect.Lists;
24 
34 public class ListMap<T> {
35  // Maps from Integer to T.
36  private List<T> list_ = Lists.newArrayList();
37  // Maps from T to Integer.
38  private final Map<T, Integer> map_ = Maps.newHashMap();
39 
40  public List<T> getList() { return list_; }
41  public int size() { return list_.size(); }
42 
46  public T getEntry(Integer index) { return list_.get(index); }
47 
52  public Integer getIndex(T t) {
53  Integer index = map_.get(t);
54  if (index == null) {
55  // No match was found, add a new entry.
56  list_.add(t);
57  index = list_.size() - 1;
58  map_.put(t, index);
59  }
60  return index;
61  }
62 
67  public void populate(List<T> list) {
68  Preconditions.checkState(list_.isEmpty() && map_.isEmpty());
69  // Require list to be an ArrayList so that getEntry() is fast.
70  Preconditions.checkState(list instanceof ArrayList<?>);
71  list_ = list;
72  for (int i = 0; i < list_.size(); ++i) {
73  map_.put(list_.get(i), i);
74  }
75  }
76 }
void populate(List< T > list)
Definition: ListMap.java:67