1 package org.apache.maven.surefire.booter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayInputStream;
23 import java.io.File;
24 import java.util.Properties;
25
26 import static org.apache.maven.surefire.util.ReflectionUtils.loadClass;
27
28
29
30
31 public class TypeEncodedValue
32 {
33 private final String type;
34 private final String value;
35
36 public TypeEncodedValue( String type, String value )
37 {
38 this.type = type;
39 this.value = value;
40 }
41
42 public boolean isTypeClass()
43 {
44 return Class.class.getName().equals( type );
45 }
46
47 public Object getDecodedValue()
48 {
49 return getDecodedValue( Thread.currentThread().getContextClassLoader() );
50 }
51
52 public Object getDecodedValue( ClassLoader classLoader )
53 {
54
55 if ( type.trim().length() == 0 )
56 {
57 return null;
58 }
59 else if ( type.equals( String.class.getName() ) )
60 {
61 return value;
62 }
63 else if ( isTypeClass() )
64 {
65 return loadClass( classLoader, value );
66 }
67 else if ( type.equals( File.class.getName() ) )
68 {
69 return new File( value );
70 }
71 else if ( type.equals( Boolean.class.getName() ) )
72 {
73 return Boolean.valueOf( value );
74 }
75 else if ( type.equals( Integer.class.getName() ) )
76 {
77 return Integer.valueOf( value );
78 }
79 else if ( type.equals( Properties.class.getName() ) )
80 {
81 Properties result = new Properties();
82
83 try
84 {
85 result.load( new ByteArrayInputStream( value.getBytes( "8859_1" ) ) );
86 return result;
87 }
88 catch ( Exception e )
89 {
90 throw new IllegalStateException( "bug in property conversion", e );
91 }
92 }
93 else
94 {
95 throw new IllegalArgumentException( "Unknown parameter type: " + type );
96 }
97 }
98
99 public boolean equals( Object o )
100 {
101 if ( this == o )
102 {
103 return true;
104 }
105 if ( o == null || getClass() != o.getClass() )
106 {
107 return false;
108 }
109
110 TypeEncodedValue that = (TypeEncodedValue) o;
111
112 return equalsType( that ) && equalsValue( that );
113
114 }
115
116 public int hashCode()
117 {
118 int result = type != null ? type.hashCode() : 0;
119 result = 31 * result + ( value != null ? value.hashCode() : 0 );
120 return result;
121 }
122
123 private boolean equalsType( TypeEncodedValue that )
124 {
125 return type == null ? that.type == null : type.equals( that.type );
126 }
127
128 private boolean equalsValue( TypeEncodedValue that )
129 {
130 return value == null ? that.value == null : value.equals( that.value );
131 }
132 }