ListInteger2ArrayInt.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.jaxb;

  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import javax.xml.bind.annotation.adapters.XmlAdapter;

  24. /**
  25.  * ListInteger2ArrayInt
  26.  *
  27.  *
  28.  * @author Nardi Lorenzo (nardi@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class ListInteger2ArrayInt extends XmlAdapter<List<Integer>, int[]>
  33. {
  34.     @Override
  35.     public List<Integer> marshal(int[] i) throws Exception {
  36.         if(i==null || i.length<=0){
  37.             return null;
  38.         }
  39.         List<Integer> list = new ArrayList<Integer>();
  40.         for(int j=0; j<i.length; j++)
  41.             list.add(i[j]);
  42.         return list;
  43.     }
  44.     @Override
  45.     public int[] unmarshal(List<Integer> list) throws Exception {
  46.         if(list==null || list.size()<=0){
  47.             return null;
  48.         }
  49.         int[] array = new int[list.size()];
  50.         for(int i=0; i<list.size(); i++){
  51.             try {
  52.                 array[i] = list.get(i).intValue();
  53.             } catch (NullPointerException p) {
  54.                 array[i] = 0;
  55.             }
  56.         }
  57.         return array;
  58.     }
  59. }