Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file 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.
  */
 package com.amazonaws.services.simpledb.util;
 
 import java.util.Date;

Provides collection of static functions for conversion of various values into strings that may be compared lexicographically.
 
 public class SimpleDBUtils {

    
static value hardcoding date format used for conversation of Date into String
 
     private static String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";


    
Encodes positive integer value into a string by zero-padding number up to the specified number of digits.

Parameters:
number positive integer to be encoded
maxNumDigits maximum number of digits in the largest value in the data set
Returns:
string representation of the zero-padded integer
 
     public static String encodeZeroPadding(int numberint maxNumDigits) {
         String integerString = Integer.toString(number);
         int numZeroes = maxNumDigits - integerString.length();
         StringBuffer strBuffer = new StringBuffer(numZeroes + integerString.length());
         for (int i = 0; i < numZeroesi++) {
             strBuffer.insert(i'0');
         }
         strBuffer.append(integerString);
         return strBuffer.toString();
     }

    
Encodes positive long value into a string by zero-padding the value up to the specified number of digits.

Parameters:
number positive long to be encoded
maxNumDigits maximum number of digits in the largest value in the data set
Returns:
string representation of the zero-padded long
 
     public static String encodeZeroPadding(long numberint maxNumDigits) {
         String longString = Long.toString(number);
         int numZeroes = maxNumDigits - longString.length();
         StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length());
         for (int i = 0; i < numZeroesi++) {
             strBuffer.insert(i'0');
         }
         strBuffer.append(longString);
         return strBuffer.toString();
     }

    
Encodes positive float value into a string by zero-padding number up to the specified number of digits

Parameters:
number positive float value to be encoded
maxNumDigits maximum number of digits preceding the decimal point in the largest value in the data set
Returns:
string representation of the zero-padded float value
 
     public static String encodeZeroPadding(float numberint maxNumDigits) {
         String floatString = Float.toString(number);
         int numBeforeDecimal = floatString.indexOf('.');
         numBeforeDecimal = (numBeforeDecimal >= 0 ? numBeforeDecimal : floatString.length());
         int numZeroes = maxNumDigits - numBeforeDecimal;
         StringBuffer strBuffer = new StringBuffer(numZeroes + floatString.length());
         for (int i = 0; i < numZeroesi++) {
             strBuffer.insert(i'0');
         }
         strBuffer.append(floatString);
         return strBuffer.toString();
     }

    
Decodes zero-padded positive integer value from the string representation

Parameters:
value zero-padded string representation of the integer
Returns:
original integer value
    public static int decodeZeroPaddingInt(String value) {
        return Integer.parseInt(value, 10);
    }

    
Decodes a zero-padded positive long value from the string representation

Parameters:
value zero-padded string representation of the long
Returns:
original long value
    public static long decodeZeroPaddingLong(String value) {
        return Long.parseLong(value, 10);
    }

    
Decodes zero-padded positive float value from the string representation

Parameters:
value zero-padded string representation of the float value
Returns:
original float value
    public static float decodeZeroPaddingFloat(String value) {
        return Float.valueOf(value).floatValue();
    }

    
Encodes real integer value into a string by offsetting and zero-padding number up to the specified number of digits. Use this encoding method if the data range set includes both positive and negative values.

Parameters:
number integer to be encoded
maxNumDigits maximum number of digits in the largest absolute value in the data set
offsetValue offset value, has to be greater than absolute value of any negative number in the data set.
Returns:
string representation of the integer
    public static String encodeRealNumberRange(int numberint maxNumDigits,
            int offsetValue) {
        long offsetNumber = number + offsetValue;
        String longString = Long.toString(offsetNumber);
        int numZeroes = maxNumDigits - longString.length();
        StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length());
        for (int i = 0; i < numZeroesi++) {
            strBuffer.insert(i'0');
        }
        strBuffer.append(longString);
        return strBuffer.toString();
    }

    
Encodes a positive or negative long value into a string by offsetting and zero-padding the number up to the specified number of digits. Use this encoding method if the data set includes both positive and negative values.

Parameters:
number positive or negative long value to be encoded
maxNumDigits maximum number of digits in the largest absolute value in the data set
offsetValue offset value, has to be greater than absolute value of any negative number in the data set.
Returns:
string representation of the long
    public static String encodeRealNumberRange(long numberint maxNumDigits,
            long offsetValue) {
        long offsetNumber = number + offsetValue;
        String longString = Long.toString(offsetNumber);
        int numZeroes = maxNumDigits - longString.length();
        StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length());
        for (int i = 0; i < numZeroesi++) {
            strBuffer.insert(i'0');
        }
        strBuffer.append(longString);
        return strBuffer.toString();
    }

    
Encodes real float value into a string by offsetting and zero-padding number up to the specified number of digits. Use this encoding method if the data range set includes both positive and negative values.

Parameters:
number float to be encoded
maxDigitsLeft maximum number of digits left of the decimal point in the largest absolute value in the data set
maxDigitsRight maximum number of digits right of the decimal point in the largest absolute value in the data set, i.e. precision
offsetValue offset value, has to be greater than absolute value of any negative number in the data set.
Returns:
string representation of the integer
    public static String encodeRealNumberRange(float numberint maxDigitsLeft,
            int maxDigitsRightint offsetValue) {
        int shiftMultiplier = (int) Math.pow(10, maxDigitsRight);
        long shiftedNumber = (long) Math.round((double)number * shiftMultiplier);
        long shiftedOffset = offsetValue * shiftMultiplier;
        long offsetNumber = shiftedNumber + shiftedOffset;
        if (offsetNumber < 0)
        {
            throw new IllegalArgumentException("OffsetNumber[" + offsetNumber + "] is negative - Number[" + number + "], maxDigitsLeft[" + maxDigitsLeft + "], maxDigitsRight[" + maxDigitsRight + "], offsetValue[" + offsetValue + "]");
        }
        String longString = Long.toString(offsetNumber);
        int numBeforeDecimal = longString.length();
        int numZeroes = maxDigitsLeft + maxDigitsRight - numBeforeDecimal;
        if (numZeroes < 0)
        {
            throw new IllegalArgumentException("Number[" + number + "] has too many digits - maxDigitsLeft[" + maxDigitsLeft + "], maxDigitsRight[" + maxDigitsRight + "], offsetValue[" + offsetValue + "]");
        }
        StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length());
        for (int i = 0; i < numZeroesi++) {
            strBuffer.insert(i'0');
        }
        strBuffer.append(longString);
        return strBuffer.toString();
    }

    
Decodes integer value from the string representation that was created by using encodeRealNumberRange(..) function.

Parameters:
value string representation of the integer value
offsetValue offset value that was used in the original encoding
Returns:
original integer value
    public static int decodeRealNumberRangeInt(String valueint offsetValue) {
        long offsetNumber = Long.parseLong(value, 10);
        return (int) (offsetNumber - offsetValue);
    }

    
Decodes a long value from the string representation that was created by using encodeRealNumberRange(..) function.

Parameters:
value string representation of the long value
offsetValue offset value that was used in the original encoding
Returns:
original long value
    public static long decodeRealNumberRangeLong(String valuelong offsetValue) {
        long offsetNumber = Long.parseLong(value, 10);
        return (long) (offsetNumber - offsetValue);
    }

    
Decodes float value from the string representation that was created by using encodeRealNumberRange(..) function.

Parameters:
value string representation of the integer value
maxDigitsRight maximum number of digits left of the decimal point in the largest absolute value in the data set (must be the same as the one used for encoding).
offsetValue offset value that was used in the original encoding
Returns:
original float value
    public static float decodeRealNumberRangeFloat(String value,
            int maxDigitsRightint offsetValue) {
        long offsetNumber = Long.parseLong(value, 10);
        int shiftMultiplier = (int) Math.pow(10, maxDigitsRight);
        double tempVal = (double) (offsetNumber - offsetValue * shiftMultiplier);
        return (float) (tempVal / (double) (shiftMultiplier));
    }

    
Encodes date value into string format that can be compared lexicographically

Parameters:
date date value to be encoded
Returns:
string representation of the date value
    public static String encodeDate(Date date) {
        SimpleDateFormat dateFormatter = new SimpleDateFormat();
        /* Java doesn't handle ISO8601 nicely: need to add ':' manually */
        String result = dateFormatter.format(date);
        return result.substring(0, result.length() - 2) + ":"
                + result.substring(result.length() - 2);
    }

    
Decodes date value from the string representation created using encodeDate(..) function.

Parameters:
value string representation of the date value
Returns:
original date value
    public static Date decodeDate(String valuethrows ParseException {
        String javaValue = value.substring(0, value.length() - 3)
                + value.substring(value.length() - 2);
        SimpleDateFormat dateFormatter = new SimpleDateFormat();
        return dateFormatter.parse(javaValue);
    }

    
Quotes and escapes a list of values so that they can be used in a SimpleDB query.

Parameters:
values The collection of attribute values that will be quoted, escaped, and included in the returned string list.
Returns:
A string representation of the list of specified values, with individual values properly quoted and escaped.
    public static String quoteValues(Collection<Stringvalues) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (String s : values) {
            if (!firstsb.append(",");
            first = false;
            sb.append(quoteValue(s));
        }
        return sb.toString();
    }

    
Quotes and escapes an attribute value by wrapping it with single quotes and escaping any single quotes inside the value.

Parameters:
value The attribute value to quote and escape.
Returns:
The properly quoted and escaped attribute value, ready to be used in a SimpleDB select query.
    public static String quoteValue(String value) {
        return "'" + replaceCharvalue"'""''" ) + "'";
    }

    
Quotes and escapes an attribute name or domain name by wrapping it with backticks and escaping any backticks inside the name.

Parameters:
name The attribute name or domain name to quote and escape.
Returns:
The properly quoted and escaped attribute name or domain name, ready to be used in a SimpleDB select query.
    public static String quoteName(String name) {
        return "`" + replaceCharname"`""``" ) + "`";
    }
    protected static String replaceCharString valueString termToFindString replacementTerm ) {
        StringBuilder buffer = new StringBuildervalue );
        int searchIndex = 0;
        while ( searchIndex < buffer.length() ) {
            searchIndex = buffer.indexOftermToFindsearchIndex );
            if ( searchIndex == -1 ) {
                break;
            }
            else {
                buffer.replacesearchIndexsearchIndex + termToFind.length(), replacementTerm );
                searchIndex += replacementTerm.length();
            }
        }
        return buffer.toString();
    }
New to GrepCode? Check out our FAQ X