001package 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.
002
003import votorola.g.lang.*;
004
005
006/** Array utilities.  Alternatives to PreparedStatement.setArray().  To use setArray(),
007  * one must implement a compatible java.sql.Array instance, and this requires
008  * documentation or support from the JDBC driver that appears to be missing for
009  * PostgreSQL.  Hence these alternative utilities for converting between arrays (which we
010  * cannot store) and strings (which we can).
011  */
012public @ThreadSafe final class ArrayX
013{
014
015
016    private ArrayX() {}
017
018
019
020    /** Returns the space-delimited concatenation of the specified int array.
021      *
022      *     @return space-delimited concatenation of intArray;
023      *       or null, if the intArray is empty
024      *
025      *     @see #stringToInts(String)
026      */
027    public static String intsToString( final int[] intArray )
028    {
029        if( intArray.length == 0 ) return null;
030
031        final StringBuilder b = new StringBuilder( /*initial capacity, guess*/100 );
032        for( int i = 0;; )
033        {
034            b.append( intArray[i] );
035            ++i;
036            if( i < intArray.length ) b.append( ' ' );
037            else break;
038        }
039        return b.toString();
040    }
041
042
043        /** Splits the space-delimited string (s) of integers into an array.
044          *
045          *     @param s space-delimited string of integers,
046          *       which may be empty or null (in which cases, an empty array is returned)
047          *
048          *     @see #intsToString(int[])
049          */
050        public static int[] stringToInts( final String s )
051        {
052            if( s == null ) return new int[0];
053
054            final String[] stringArray = s.split( " " );
055            final int[] intArray = new int[stringArray.length];
056            for( int i = intArray.length - 1; i >= 0; --i )
057            {
058                intArray[i] = Integer.parseInt( stringArray[i] );
059            }
060            return intArray;
061        }
062
063
064
065    /** Returns the space-delimited concatenation of the specified string array.
066      *
067      *     @return space-delimited concatenation of stringArray;
068      *       or null, if the stringArray is empty
069      *
070      *     @see #stringToStrings(String)
071      */
072    public static String stringsToString( final String[] stringArray )
073    {
074        if( stringArray.length == 0 ) return null;
075
076        final StringBuilder b = new StringBuilder( /*initial capacity, guess*/100 );
077        for( int i = 0;; )
078        {
079            b.append( stringArray[i] );
080            ++i;
081            if( i < stringArray.length ) b.append( ' ' );
082            else break;
083        }
084        return b.toString();
085    }
086
087
088        /** Splits the space-delimited string (s) of strings into an array.
089          *
090          *     @param s space-delimited string of strings, which may be empty or null
091          *       (in which cases, an empty array is returned)
092          *
093          *     @see #stringsToString(String[])
094          */
095        public static String[] stringToStrings( final String s )
096        {
097            if( s == null ) return new String[0];
098
099            return s.split( " " );
100        }
101
102
103
104}