Here you can find the source of getFirstString(Object list)
Parameter | Description |
---|---|
input | a parameter |
public static String getFirstString(Object list)
//package com.java2s; /**/*from w w w . java 2s . co m*/ * This document is a part of the source code and related artifacts * for CollectionSpace, an open source collections management system * for museums and related institutions: * http://www.collectionspace.org * http://wiki.collectionspace.org * Copyright 2009 University of California at Berkeley * Licensed under the Educational Community License (ECL), Version 2.0. * You may not use this file except in compliance with this License. * You may obtain a copy of the ECL 2.0 License at * https://source.collectionspace.org/collection-space/LICENSE.txt * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.List; public class Main { /** * unqualify given dateVal. input of * otherNumber|urn:org.collectionspace.id:24082390 would be unqualified as * name=otherNumber and dateVal=urn:org.collectionspace.id:24082390 * * @param input * @return name and dateVal * @exception IllegalStateException * private static NameValue unqualify(String input) { * NameValue nv = new NameValue(); StringTokenizer stz = new * StringTokenizer(input, NAME_VALUE_SEPARATOR); int tokens = * stz.countTokens(); if (tokens == 2) { nv.name = * stz.nextToken(); nv.dateVal = stz.nextToken(); // Allow * null or empty values } else if (tokens == 1) { nv.name = * stz.nextToken(); nv.dateVal = ""; } else { throw new * IllegalStateException * ("Unexpected format for multi valued element: " + input); * } return nv; } */ public static String getFirstString(Object list) { if (list == null) { return null; } if (list instanceof List) { return ((List) list).size() == 0 ? null : (String) ((List) list).get(0); } Class<?> arrType = list.getClass().getComponentType(); if ((arrType != null) && arrType.isPrimitive()) { if (arrType == Integer.TYPE) { int[] ar = (int[]) list; return ar.length == 0 ? null : String.valueOf(ar[0]); } else if (arrType == Long.TYPE) { long[] ar = (long[]) list; return ar.length == 0 ? null : String.valueOf(ar[0]); } else if (arrType == Double.TYPE) { double[] ar = (double[]) list; return ar.length == 0 ? null : String.valueOf(ar[0]); } else if (arrType == Float.TYPE) { float[] ar = (float[]) list; return ar.length == 0 ? null : String.valueOf(ar[0]); } else if (arrType == Character.TYPE) { char[] ar = (char[]) list; return ar.length == 0 ? null : String.valueOf(ar[0]); } else if (arrType == Byte.TYPE) { byte[] ar = (byte[]) list; return ar.length == 0 ? null : String.valueOf(ar[0]); } else if (arrType == Short.TYPE) { short[] ar = (short[]) list; return ar.length == 0 ? null : String.valueOf(ar[0]); } throw new IllegalArgumentException( "Primitive list of unsupported type: " + list); } throw new IllegalArgumentException( "A value of list type is neither list neither array: " + list); } }