BrightSide Workbench Full Report + Source Code
elephant-plugins/src/main/java/org/turro/plugin/contacts/IContactTreeMap.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.plugin.contacts;
19 
20 import java.util.Map;
21 import java.util.TreeMap;
22 import org.turro.action.Contacts;
23 
28 public class IContactTreeMap<C, V> extends TreeMap<IContact, V> {
29 
30  public IContactTreeMap(IContactNameComparator iContactNameComparator) {
31  super(iContactNameComparator);
32  }
33 
34  @Override
35  public V get(Object key) {
36  return super.get(getRealKey(key));
37  }
38 
39  @Override
40  public V put(IContact key, V value) {
41  return super.put(getRealKey(key), value);
42  }
43 
44  public V getById(String key) {
45  return super.get(getRealKeyById(key));
46  }
47 
48  public V putById(String key, V value) {
49  return super.put(getRealKeyById(key), value);
50  }
51 
52  @Override
53  public void putAll(Map<? extends IContact, ? extends V> map) {
54  throw new UnsupportedOperationException("Use putIContact instead");
55  }
56 
57  private IContact getRealKey(Object key) {
58  IContact ickey = (IContact) key;
59  for(IContact ic : keySet()) {
60  if(ic.getId().equals(ickey.getId())) {
61  return ic;
62  }
63  }
64  return (IContact) key;
65  }
66 
67  private IContact getRealKeyById(String key) {
68  for(IContact ic : keySet()) {
69  if(ic.getId().equals(key)) {
70  return ic;
71  }
72  }
73  return Contacts.getContact(key);
74  }
75 
76 }