MapResult.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.csv;
  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import org.openspcoop2.utils.UtilsException;

  24. /**
  25.  * MapResult
  26.  *
  27.  * @author Andrea Poli (apoli@link.it)
  28.  * @author $Author$
  29.  * @version $Rev$, $Date$
  30.  */
  31. public class MapResult {

  32.     // E' stata fatta questa classe per evitare i nullPointer nel caso di valori null se si usa una hashtable
  33.    
  34.     private List<Result> list = new ArrayList<Result>();
  35.     private List<String> keys = new ArrayList<>();
  36.    
  37.     public void add(String name,String value) throws UtilsException{
  38.        
  39.         if(this.keys.contains(name)){
  40.             Result r = this.getResult(name);
  41.             r.value = value;
  42.         }
  43.         else{
  44.             Result r = new Result();
  45.             r.name = name;
  46.             r.value = value;
  47.             this.list.add(r);
  48.             this.keys.add(name);
  49.         }
  50.     }
  51.     public String get(String name) throws UtilsException{
  52.         return this.getResult(name).value;
  53.     }
  54.     public List<String> keys(){
  55.         return this.keys;
  56.     }
  57.     public int size(){
  58.         return this.keys.size();
  59.     }
  60.    
  61.     private Result getResult(String name) throws UtilsException{
  62.         for (Result r : this.list) {
  63.             if(r.name.equals(name)){
  64.                 return r;
  65.             }
  66.         }
  67.         throw new UtilsException("Entry with key ["+name+"] not exists");
  68.     }
  69. }

  70. class Result{
  71.     String name;
  72.     String value;
  73. }