SortedMap.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.utils;

  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;

  25. /**
  26.  * SortMap
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class SortedMap<T> {

  33.     protected List<String> list = new ArrayList<>(); // per preservare l'ordine
  34.     protected Map<String, T> map = new HashMap<>();
  35.    
  36.     public int size(){
  37.         return this.list.size();
  38.     }
  39.    
  40.     public List<String> keys(){
  41.         return this.list;
  42.     }
  43.     public List<T> values(){
  44.         return new ArrayList<>(this.map.values());
  45.     }
  46.    
  47.     public void clear() {
  48.         this.list.clear();
  49.         this.map.clear();
  50.     }
  51.    
  52.     public void put(String key, T archive) throws UtilsException{
  53.         this.add(key, archive);
  54.     }
  55.     public void add(String key, T archive) throws UtilsException{
  56.         if(this.map.containsKey(key)){
  57.             throw new UtilsException("Key ["+key+"] already exists");
  58.         }
  59.         this.list.add(key);
  60.         this.map.put(key, archive);
  61.     }
  62.    
  63.     public T get(int index){
  64.         String key = this.list.get(index);
  65.         return this.map.get(key);
  66.     }
  67.     public T get(String key){
  68.         return this.map.get(key);
  69.     }
  70.    
  71.     public T remove(int index){
  72.         String key = this.list.remove(index);
  73.         return this.map.remove(key);
  74.     }
  75.     public T remove(String key){
  76.         for (int i = 0; i < this.list.size(); i++) {
  77.             if(this.list.get(i).equals(key)){
  78.                 this.list.remove(i);
  79.                 break;
  80.             }
  81.         }
  82.         return this.map.remove(key);
  83.     }
  84.    
  85.     public boolean containsKey(String key){
  86.         return this.map.containsKey(key);
  87.     }
  88.     public boolean containsValue(T value){
  89.         return this.map.containsValue(value);
  90.     }
  91.    
  92.     public boolean isEmpty() {
  93.         return this.map.isEmpty();
  94.     }
  95.    
  96. }