Amazon SimpleDB Util
/*******************************************************************************
* 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
{
/// <summary>
/// Date format String, e.g. 2007-12-06T10:32:43.141-08:00
/// </summary>
private static String dateFormat = "yyyy-MM-ddTHH:mm:ss.fffzzzz";
/// <summary>
/// Encodes positive integer value into a string by zero-padding it up to the specified number of digits.
/// </summary>
/// <remarks>
/// For example, the integer 123 encoded with a 6 digit maximum would be represented as 000123
/// </remarks>
/// <param name="number">positive integer to be encoded</param>
/// <param name="maxNumDigits">maximum number of digits in the largest value in the data set</param>
/// <returns>A string representation of the zero-padded integer</returns>
public static String EncodeZeroPadding(int number, int maxNumDigits)
{
return number.ToString().PadLeft(maxNumDigits, '0');
}
/// <summary>
/// Encodes positive single-precision floating point value into a string by zero-padding it to the specified number of digits.
/// </summary>
/// <remarks>
/// 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
/// </remarks>
/// <param name="number">positive floating point value to be encoded</param>
/// <param name="maxNumDigits">maximum number of digits in the largest value in the data set</param>
/// <returns>A string representation of the zero-padded floating point value</returns>
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');
}
}
/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// For example, the integer value -123 offset by 1000 with a maximum of 6 digits would be:
/// -123 + 1000, padded to 6 digits: 000877
/// </remarks>
/// <param name="number">integer to be encoded</param>
/// <param name="maxNumDigits">maximum number of digits in the largest absolute value in the data set</param>
/// <param name="offsetValue">offset value, has to be greater than absolute value of any negative number in the data set.</param>
/// <returns>A string representation of the integer</returns>
public static String EncodeRealNumberRange(int number, int maxNumDigits, int offsetValue)
{
return (number + offsetValue).ToString().PadLeft(maxNumDigits, '0');
}
/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// 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
/// </remarks>
/// <param name="number">floating point value to be encoded</param>
/// <param name="maxDigitsLeft">maximum number of digits left of the decimal point in the largest absolute value in the data set</param>
/// <param name="maxDigitsRight">maximum number of digits right of the decimal point in the largest absolute value in the data set, i.e. precision</param>
/// <param name="offsetValue">offset value, has to be greater than absolute value of any negative number in the data set.</param>
/// <returns>A string representation of the integer</returns>
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');
}
/// <summary>
/// Decodes zero-padded positive float value from the string representation
/// </summary>
/// <param name="value">zero-padded string representation of the float value</param>
/// <returns>original float value</returns>
public static float DecodeZeroPaddingFloat(String value)
{
return float.Parse(value);
}
/// <summary>
/// Decodes zero-padded positive integer value from the string representation
/// </summary>
/// <param name="value">zero-padded string representation of the integer</param>
/// <returns>original integer value</returns>
public static int DecodeZeroPaddingInt(String value)
{
return int.Parse(value);
}
/// <summary>
/// Decodes float value from the string representation that was created by using encodeRealNumberRange(..) function.
/// </summary>
/// <param name="value">String representation of the integer value</param>
/// <param name="offsetValue">offset value that was used in the original encoding</param>
/// <returns>original integer value</returns>
public static int DecodeRealNumberRangeInt(String value, int offsetValue)
{
return (int)(long.Parse(value) - offsetValue);
}
/// <summary>
/// Decodes float value from the string representation that was created by using encodeRealNumberRange(..) function.
/// </summary>
/// <param name="value">string representation of the integer value</param>
/// <param name="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).</param>
/// <param name="offsetValue">offset value that was used in the original encoding</param>
/// <returns>original float value</returns>
public static float DecodeRealNumberRangeFloat(String value, int maxDigitsRight, int offsetValue)
{
return (float)(long.Parse(value) / Math.Pow(10, maxDigitsRight) - offsetValue);
}
/// <summary>
/// Encodes date value into string format that can be compared lexicographically
/// </summary>
/// <param name="date">date value to be encoded</param>
/// <returns>string representation of the date value</returns>
public static String EncodeDate(DateTime date)
{
return date.ToString(dateFormat);
}
/// <summary>
/// Decodes date value from the string representation created using encodeDate(..) function.
/// </summary>
/// <param name="value">string representation of the date value</param>
/// <returns>original date value</returns>
public static DateTime DecodeDate(String value)
{
return DateTime.ParseExact(value, dateFormat, CultureInfo.InvariantCulture);
}
}
}
Related examples in the same category
1. | use the Format() method to format a string | | |
2. | Use the static String.Format() method to build a new string. | | |
3. | Fill placeholders using an array of objects. | | |
4. | Format a string | | |
5. | Use string.Format to format integer | | |
6. | The comma (,M) determines the field width and justification. | | |
7. | Control the width | | |
8. | left justify and align a set of strings to improve the appearance of program output | | |
9. | |{0,10:X}|{1,10}|{2:X}|{3}| | | |
10. | {0,4} {1,4} {2,4} {3,4} {4,4} | | |
11. | Format with {0:F} | | |
12. | Formats a string to an invariant culture | | |
13. | Formats a string to the current culture. | | |
14. | Clean \t (tab), \r from strings | | |
15. | Pad String | | |
16. | Convert the string e.g. fooBar to sentance case: FooBar | | |
17. | Formats the specified size as a string. | | |
18. | Converts a space delimited string into a single, compound pascal case string | | |
19. | To String Camel Case | | |
20. | Format Array To Comma Delimited String | | |
21. | Split the multi-line output into separate line strings | | |
22. | Escape and unescape string | | |
23. | Convert Size to String | | |
24. | Format the given string using the provided collection of objects. | | |
25. | Get a string representation of flags. | | |
26. | Reads count number of characters and returns them as a string with any null terminators removed. | | |
27. | Truncate On Word Boundary | | |
28. | Camel/uncamel cases the specified input | | |
29. | Camel Case | | |
30. | To First Upper Case | | |
31. | To Pascal Case | | |
32. | Split Camel Case | | |
33. | Proper Case | | |
34. | Strips all illegal characters from the specified title | | |
35. | Appends a space before all capital letters in a sentence, except the first character. | | |
36. | Remove Illegal Characters | | |
37. | Remove Diacritics | | |
38. | StripSpaces removes spaces at the beginning and at the end of the value and replaces sequences of spaces with a single space | | |
39. | Display value in a grid | | |
40. | Get the last word | | |
41. | Implementation of the Infelctor in Ruby that transforms words from singular to plural | | |
42. | Strips all illegal characters from the specified title. | | |