package votorola.g.sql; // Copyright 2008, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import votorola.g.lang.*; /** Array utilities. Alternatives to PreparedStatement.setArray(). To use setArray(), * one must implement a compatible java.sql.Array instance, and this requires * documentation or support from the JDBC driver that appears to be missing for * PostgreSQL. Hence these alternative utilities for converting between arrays (which we * cannot store) and strings (which we can). */ public @ThreadSafe final class ArrayX { private ArrayX() {} /** Returns the space-delimited concatenation of the specified int array. * * @return space-delimited concatenation of intArray; * or null, if the intArray is empty * * @see #stringToInts(String) */ public static String intsToString( final int[] intArray ) { if( intArray.length == 0 ) return null; final StringBuilder b = new StringBuilder( /*initial capacity, guess*/100 ); for( int i = 0;; ) { b.append( intArray[i] ); ++i; if( i < intArray.length ) b.append( ' ' ); else break; } return b.toString(); } /** Splits the space-delimited string (s) of integers into an array. * * @param s space-delimited string of integers, * which may be empty or null (in which cases, an empty array is returned) * * @see #intsToString(int[]) */ public static int[] stringToInts( final String s ) { if( s == null ) return new int[0]; final String[] stringArray = s.split( " " ); final int[] intArray = new int[stringArray.length]; for( int i = intArray.length - 1; i >= 0; --i ) { intArray[i] = Integer.parseInt( stringArray[i] ); } return intArray; } /** Returns the space-delimited concatenation of the specified string array. * * @return space-delimited concatenation of stringArray; * or null, if the stringArray is empty * * @see #stringToStrings(String) */ public static String stringsToString( final String[] stringArray ) { if( stringArray.length == 0 ) return null; final StringBuilder b = new StringBuilder( /*initial capacity, guess*/100 ); for( int i = 0;; ) { b.append( stringArray[i] ); ++i; if( i < stringArray.length ) b.append( ' ' ); else break; } return b.toString(); } /** Splits the space-delimited string (s) of strings into an array. * * @param s space-delimited string of strings, which may be empty or null * (in which cases, an empty array is returned) * * @see #stringsToString(String[]) */ public static String[] stringToStrings( final String s ) { if( s == null ) return new String[0]; return s.split( " " ); } }