litesoft
@ 939
litesoft / trunk / DeviceDesktopTest / src / org / litesoft / core / util / Problem.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.core.util; import org.litesoft.core.annotations.*; import org.litesoft.core.typeutils.*; import java.io.*; import org.litesoft.core.typeutils.Objects; import java.util.*; public class Problem implements Serializable { private static final long serialVersionUID = 2L; private /* final */ String mProblemCode; private /* final */ String[] mProblemSupportValues; private /* final */ List<NameValuePair> mNamedSupportValues; @Deprecated /** for Serialization */ protected Problem() { } public Problem( @NotNull Throwable pThrowable ) { this( pThrowable, null, (String[]) null ); } public Problem( @NotNull String pProblemCode ) { this( null, pProblemCode, (String[]) null ); } public Problem( @NotNull String pProblemCode, String[] pProblemSupportValues ) { this( null, pProblemCode, pProblemSupportValues ); } public Problem( @NotNull String pProblemCode, String pProblemSupportValue ) { this( null, pProblemCode, new String[]{pProblemSupportValue} ); } public Problem( @NotNull String pProblemCode, String pProblemSupportValue0, String pProblemSupportValue1 ) { this( null, pProblemCode, new String[]{pProblemSupportValue0, pProblemSupportValue1} ); } public Problem( @NotNull String pProblemCode, String pProblemSupportValue0, String pProblemSupportValue1, String pProblemSupportValue2 ) { this( null, pProblemCode, new String[]{pProblemSupportValue0, pProblemSupportValue1, pProblemSupportValue2} ); } public Problem( @NotNull String pProblemCode, String pProblemSupportValue0, String pProblemSupportValue1, String pProblemSupportValue2, String pProblemSupportValue3 ) { this( null, pProblemCode, new String[]{pProblemSupportValue0, pProblemSupportValue1, pProblemSupportValue2, pProblemSupportValue3} ); } public Problem( Throwable pThrowable, String pProblemCode ) { this( pThrowable, pProblemCode, (String[]) null ); } public Problem( Throwable pThrowable, String pProblemCode, String pProblemSupportValue ) { this( pThrowable, pProblemCode, new String[]{pProblemSupportValue} ); } public Problem( Throwable pThrowable, String pProblemCode, String pProblemSupportValue0, String pProblemSupportValue1 ) { this( pThrowable, pProblemCode, new String[]{pProblemSupportValue0, pProblemSupportValue1} ); } public Problem( Throwable pThrowable, String pProblemCode, String pProblemSupportValue0, String pProblemSupportValue1, String pProblemSupportValue2 ) { this( pThrowable, pProblemCode, new String[]{pProblemSupportValue0, pProblemSupportValue1, pProblemSupportValue2} ); } public Problem( Throwable pThrowable, String pProblemCode, String pProblemSupportValue0, String pProblemSupportValue1, String pProblemSupportValue2, String pProblemSupportValue3 ) { this( pThrowable, pProblemCode, new String[]{pProblemSupportValue0, pProblemSupportValue1, pProblemSupportValue2, pProblemSupportValue3} ); } /** * One of <code>pProblemCode</code> or <code>pThrowable</code> must be specified. * * @param pThrowable a Throwable or <code>null</code> * @param pProblemCode * @param pProblemSupportValues additional info about <code>pProblemCode</code>, or <code>null</code> * * @throws IllegalArgumentException if <code>pProblemSupportValues</code> is * specified without <code>pProblemCode</code> */ public Problem( Throwable pThrowable, String pProblemCode, String[] pProblemSupportValues ) { this( pThrowable, pProblemCode, pProblemSupportValues, null ); } private Problem( Throwable pThrowable, String pProblemCode, String[] pProblemSupportValues, List<NameValuePair> pNamedSupportValues ) { pProblemCode = Strings.noEmpty( pProblemCode ); pProblemSupportValues = Strings.noEmpty( pProblemSupportValues ); pNamedSupportValues = Lists.deNull( pNamedSupportValues ); if ( (pThrowable == null) && (pProblemCode == null) ) { throw new IllegalArgumentException( "Either the Throwable or the ProblemCode must not be null" ); } if ( (pProblemCode == null) && ((pProblemSupportValues != null) || !pNamedSupportValues.isEmpty()) ) { throw new IllegalArgumentException( "...SupportValues not allowed when there is no ProblemCode" ); } List<String> zProblemSupportValues = Lists.newArrayList(); if ( pProblemSupportValues != null ) { zProblemSupportValues.addAll( Arrays.asList( pProblemSupportValues ) ); } if ( pThrowable != null ) { if ( pProblemCode == null ) { pProblemCode = pThrowable.getMessage(); } else { zProblemSupportValues.add( pThrowable.getMessage() ); } zProblemSupportValues.add( Throwables.printStackTraceToString( pThrowable ) ); } mProblemCode = pProblemCode; mProblemSupportValues = zProblemSupportValues.toArray( new String[zProblemSupportValues.size()] ); mNamedSupportValues = Lists.deNullImmutable( pNamedSupportValues ); } public @NotNull String getProblemCode() { return mProblemCode; } public @NotNull String[] getProblemSupportValues() { return mProblemSupportValues; } public @NotNull @Immutable List<NameValuePair> getNamedSupportValues() { return mNamedSupportValues; } public static class Builder { private final String mProblemCode; private final List<NameValuePair> mPairs = Lists.newArrayList(); private final Set<String> mNames = Sets.newHashSet(); public Builder( String pProblemCode ) { mProblemCode = Strings.noEmpty( "ProblemCode", pProblemCode ); } public Builder add( String pName, Object pValue ) { if ( !mNames.add( pName = Strings.assertNotNullNotEmpty( "Name", pName ) ) ) { throw new IllegalArgumentException( "Duplicate Name: " + pName ); } String zValue = (pValue == null) ? null : pValue.toString(); mPairs.add( new NameValuePair( pName, zValue ) ); return this; } public Problem build() { return new Problem( null, mProblemCode, null, mPairs ); } } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append( mProblemCode ); if ( mProblemSupportValues != null ) // null during construction! { String zOrigPrepend = "( "; String zPrepend = zOrigPrepend; for ( String zValue : mProblemSupportValues ) { sb.append( zPrepend ).append( Strings.quote( zValue ) ); zPrepend = ", "; } if ( !zPrepend.equals( zOrigPrepend ) ) { sb.append( " )" ); } } return (sb.length() == 0) ? "?" : sb.toString(); } } |