001package burrows.api.finance.model; 002 003/** 004 * An enumeration that represents the formats that the API supports. 005 * 006 * @author <a href="mailto:jaredsburrows@gmail.com">Jared Burrows</a> 007 * @since 0.0.1 008 */ 009public enum Format { 010 DEFAULT(0, "DEFAULT"), 011 // TODO not supported 012 XML(1, "XML"), 013 JSON(2, "JSON"); 014 015 private final int intValue; 016 private final String stringValue; 017 018 private Format(final int intValue, final String stringValue) { 019 this.intValue = intValue; 020 this.stringValue = stringValue; 021 } 022 023 /** 024 * Returns the integer value of this enum instance. 025 * 026 * @return the integer value of this enum instance. 027 * 028 * @see #fromInt(int) 029 * @since 0.0.1 030 */ 031 public int getIntValue() { 032 return this.intValue; 033 } 034 035 /** 036 * Returns the underlying {@link #stringValue} of this instance. 037 * 038 * @return the underlying {@link #stringValue} of this instance. 039 * 040 * @see #fromString(String) 041 * @since 0.0.1 042 */ 043 public String getStringValue() { 044 return this.stringValue; 045 } 046 047 /** 048 * Returns the integer value of this enum instance. 049 * 050 * @param intValue the integer value to determine the matching enum instance for. 051 * 052 * @return the enum instance for the specified int value, or {@code null} if there is no match. 053 * 054 * @see #getIntValue() 055 * @since 0.0.1 056 */ 057 public static Format fromInt(int intValue) { 058 for (Format instance : values()) { 059 if (instance.intValue == intValue) { 060 return instance; 061 } 062 } 063 return null; 064 } 065 066 /** 067 * Returns the enum instance for the specified string. 068 * 069 * @param stringValue the string value to determine the matching enum instance for. 070 * 071 * @return the enum instance for the specified string, or {@code null} if there is no match. 072 * 073 * @see #getStringValue() 074 * @since 0.0.1 075 */ 076 public static Format fromString(String stringValue) { 077 if (stringValue == null || stringValue.length() == 0) { 078 return null; 079 } 080 081 for (Format instance : values()) { 082 if (instance.stringValue.equalsIgnoreCase(stringValue)) { 083 return instance; 084 } 085 } 086 return null; 087 } 088}