001 package com.croftsoft.apps.agoracast.c2p; 002 003 import java.util.*; 004 005 import com.croftsoft.core.lang.NullArgumentException; 006 import com.croftsoft.core.text.ParseLib; 007 import com.croftsoft.core.text.sml.SmlNode; 008 009 /********************************************************************* 010 * 011 * <p /> 012 * 013 * @version 014 * 2001-11-09 015 * @since 016 * 2001-08-08 017 * @author 018 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 019 *********************************************************************/ 020 021 public final class AgoracastField 022 ////////////////////////////////////////////////////////////////////// 023 ////////////////////////////////////////////////////////////////////// 024 { 025 026 public static final String SML_NODE_NAME = "field"; 027 028 public static final int TYPE_NUMBER = 0; 029 030 public static final int TYPE_STRING = 1; 031 032 // 033 034 private final String name; 035 036 private final String value; 037 038 private final int type; 039 040 private final boolean isReverse; 041 042 private final String [ ] choices; 043 044 private final String semantic; 045 046 ////////////////////////////////////////////////////////////////////// 047 ////////////////////////////////////////////////////////////////////// 048 049 public static AgoracastField fromSmlNode ( SmlNode smlNode ) 050 ////////////////////////////////////////////////////////////////////// 051 { 052 if ( !SML_NODE_NAME.equals ( smlNode.getName ( ) ) ) 053 { 054 throw new IllegalArgumentException ( smlNode.getName ( ) ); 055 } 056 057 String name = smlNode.getString ( "name" ); 058 059 String value = smlNode.getString ( "value" ); 060 061 int type = smlNode.getInt ( "type", TYPE_STRING ); 062 063 boolean isReverse = smlNode.getBoolean ( "isReverse", false ); 064 065 String [ ] choices = smlNode.getStrings ( "choice" ); 066 067 String semantic = smlNode.getString ( "semantic" ); 068 069 return new AgoracastField ( 070 name, value, type, isReverse, choices, semantic ); 071 } 072 073 ////////////////////////////////////////////////////////////////////// 074 ////////////////////////////////////////////////////////////////////// 075 076 public AgoracastField ( 077 String name, 078 String value, 079 int type, 080 boolean isReverse, 081 String [ ] choices, 082 String semantic ) 083 ////////////////////////////////////////////////////////////////////// 084 { 085 NullArgumentException.check ( this.name = name ); 086 087 this.value = value; 088 089 this.type = type; 090 091 this.isReverse = isReverse; 092 093 this.choices = choices; 094 095 this.semantic = semantic; 096 097 if ( type < 0 || type > 1 ) 098 { 099 throw new IllegalArgumentException ( "unknown type: " + type ); 100 } 101 } 102 103 ////////////////////////////////////////////////////////////////////// 104 ////////////////////////////////////////////////////////////////////// 105 106 public String getName ( ) { return name; } 107 108 public String getValue ( ) { return value; } 109 110 public int getType ( ) { return type; } 111 112 public boolean isReverse ( ) { return isReverse; } 113 114 public String [ ] getChoices ( ) { return choices; } 115 116 public String getSemantic ( ) { return semantic; } 117 118 ////////////////////////////////////////////////////////////////////// 119 ////////////////////////////////////////////////////////////////////// 120 121 public int compare ( 122 String value1, 123 String value2 ) 124 ////////////////////////////////////////////////////////////////////// 125 { 126 if ( ( value1 == null ) 127 && ( value2 == null ) ) 128 { 129 return 0; 130 } 131 132 if ( value1 == null ) 133 { 134 return -1; 135 } 136 137 if ( value2 == null ) 138 { 139 return 1; 140 } 141 142 if ( type == TYPE_STRING ) 143 { 144 return value1.compareTo ( value2 ); 145 } 146 147 Double d1 = null; 148 149 try 150 { 151 String s1 = ParseLib.stripNonNumbers ( value1 ); 152 153 d1 = new Double ( s1 ); 154 } 155 catch ( NumberFormatException ex ) 156 { 157 return -1; 158 } 159 160 Double d2 = null; 161 162 try 163 { 164 String s2 = ParseLib.stripNonNumbers ( value2 ); 165 166 d2 = new Double ( s2 ); 167 } 168 catch ( NumberFormatException ex ) 169 { 170 return 1; 171 } 172 173 return d1.compareTo ( d2 ); 174 } 175 176 public synchronized SmlNode toSmlNode ( ) 177 ////////////////////////////////////////////////////////////////////// 178 { 179 SmlNode rootSmlNode = new SmlNode ( SML_NODE_NAME ); 180 181 rootSmlNode.add ( new SmlNode ( "name" , name ) ); 182 183 rootSmlNode.add ( new SmlNode ( "value", value ) ); 184 185 rootSmlNode.add ( 186 new SmlNode ( "type", Integer.toString ( type ) ) ); 187 188 rootSmlNode.add ( 189 new SmlNode ( "isReverse", isReverse ? "1" : "0" ) ); 190 191 if ( choices != null ) 192 { 193 for ( int i = 0; i < choices.length; i++ ) 194 { 195 rootSmlNode.add ( new SmlNode ( "choice", choices [ i ] ) ); 196 } 197 } 198 199 rootSmlNode.add ( new SmlNode ( "semantic", semantic ) ); 200 201 return rootSmlNode; 202 } 203 204 ////////////////////////////////////////////////////////////////////// 205 ////////////////////////////////////////////////////////////////////// 206 }