/*******************************************************************************
* Copyright 2008 Amazon Technologies, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
*
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at: http://aws.amazon.com/apache2.0
* 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.
* *****************************************************************************
* __ _ _ ___
* ( )( \/\/ )/ __)
* /__\ \ / \__ \
* (_)(_) \/\/ (___/
*
* Amazon Simple DB CSharp Library
* API Version: 2009-04-15
* Generated: Mon May 11 14:22:34 PDT 2009
*
*/
using System;
using System.Globalization;
namespace Amazon.SimpleDB.Util
{
/**
* Provides collection of static functions for conversion of various values into
* strings that may be compared lexicographically.
*/
public class AmazonSimpleDBUtil
{
///
/// Date format String, e.g. 2007-12-06T10:32:43.141-08:00
///
private static String dateFormat = "yyyy-MM-ddTHH:mm:ss.fffzzzz";
///
/// Encodes positive integer value into a string by zero-padding it up to the specified number of digits.
///
///
/// For example, the integer 123 encoded with a 6 digit maximum would be represented as 000123
///
/// positive integer to be encoded
/// maximum number of digits in the largest value in the data set
/// A string representation of the zero-padded integer
public static String EncodeZeroPadding(int number, int maxNumDigits)
{
return number.ToString().PadLeft(maxNumDigits, '0');
}
///
/// Encodes positive single-precision floating point value into a string by zero-padding it to the specified number of digits.
///
///
/// This function only zero-pads digits to the left of the decimal point.
///
/// For example, the value 123.456 encoded with a 6 digit maximum would be represented as 000123.456
///
/// positive floating point value to be encoded
/// maximum number of digits in the largest value in the data set
/// A string representation of the zero-padded floating point value
public static String EncodeZeroPadding(float number, int maxNumDigits)
{
String fltStr = number.ToString();
int decPt = fltStr.IndexOf('.');
if (decPt == -1)
{
return fltStr.PadLeft(maxNumDigits, '0');
}
else
{
return fltStr.PadLeft(maxNumDigits + (fltStr.Length - decPt), '0');
}
}
///
/// 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.
///
///
/// For example, the integer value -123 offset by 1000 with a maximum of 6 digits would be:
/// -123 + 1000, padded to 6 digits: 000877
///
/// integer to be encoded
/// maximum number of digits in the largest absolute value in the data set
/// offset value, has to be greater than absolute value of any negative number in the data set.
/// A string representation of the integer
public static String EncodeRealNumberRange(int number, int maxNumDigits, int offsetValue)
{
return (number + offsetValue).ToString().PadLeft(maxNumDigits, '0');
}
///
/// 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.
///
///
/// For example, the floating point value -123.456 offset by 1000 with
/// a maximum of 6 digits to the left, and 4 to the right would be:
/// 0008765440
///
/// floating point value to be encoded
/// maximum number of digits left of the decimal point in the largest absolute value in the data set
/// maximum number of digits right of the decimal point in the largest absolute value in the data set, i.e. precision
/// offset value, has to be greater than absolute value of any negative number in the data set.
/// A string representation of the integer
public static String EncodeRealNumberRange(float number, int maxDigitsLeft, int maxDigitsRight, int offsetValue)
{
long shiftMultiplier = (long)Math.Pow(10, maxDigitsRight);
long shiftedNumber = (long)Math.Round((number + offsetValue) * shiftMultiplier);
return shiftedNumber.ToString().PadLeft(maxDigitsLeft + maxDigitsRight, '0');
}
///
/// Decodes zero-padded positive float value from the string representation
///
/// zero-padded string representation of the float value
/// original float value
public static float DecodeZeroPaddingFloat(String value)
{
return float.Parse(value);
}
///
/// Decodes zero-padded positive integer value from the string representation
///
/// zero-padded string representation of the integer
/// original integer value
public static int DecodeZeroPaddingInt(String value)
{
return int.Parse(value);
}
///
/// Decodes float value from the string representation that was created by using encodeRealNumberRange(..) function.
///
/// String representation of the integer value
/// offset value that was used in the original encoding
/// original integer value
public static int DecodeRealNumberRangeInt(String value, int offsetValue)
{
return (int)(long.Parse(value) - offsetValue);
}
///
/// Decodes float value from the string representation that was created by using encodeRealNumberRange(..) function.
///
/// string representation of the integer value
/// 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).
/// offset value that was used in the original encoding
/// original float value
public static float DecodeRealNumberRangeFloat(String value, int maxDigitsRight, int offsetValue)
{
return (float)(long.Parse(value) / Math.Pow(10, maxDigitsRight) - offsetValue);
}
///
/// Encodes date value into string format that can be compared lexicographically
///
/// date value to be encoded
/// string representation of the date value
public static String EncodeDate(DateTime date)
{
return date.ToString(dateFormat);
}
///
/// Decodes date value from the string representation created using encodeDate(..) function.
///
/// string representation of the date value
/// original date value
public static DateTime DecodeDate(String value)
{
return DateTime.ParseExact(value, dateFormat, CultureInfo.InvariantCulture);
}
}
}