22
33import java .util .ArrayList ;
44import java .util .Arrays ;
5- import java .util .HashMap ;
65import java .util .List ;
76import java .util .Map ;
87
98import org .apache .commons .lang .StringUtils ;
109
10+ import com .google .common .collect .ImmutableList ;
11+ import com .google .common .collect .ImmutableMap ;
12+
1113/**
1214 * This class is basically a replacement of javax.ws.rs.client.WebTarget to allow simpler migration of JAX-RS code to a netty based
1315 * implementation.
1618 */
1719public class WebTarget {
1820
19- private ChannelProvider channelProvider ;
21+ private final ChannelProvider channelProvider ;
2022
21- private List <String > path = new ArrayList < String >() ;
23+ private final ImmutableList <String > path ;
2224
23- private Map <String , String > queryParams = new HashMap < String , String >() ;
25+ private final ImmutableMap <String , String > queryParams ;
2426
2527 private static final String PATH_SEPARATOR = "/" ;
2628
2729 public WebTarget (ChannelProvider channelProvider ) {
30+ this (channelProvider , ImmutableList .<String >of (), ImmutableMap .<String , String >of ());
31+ }
32+
33+ private WebTarget (ChannelProvider channelProvider ,
34+ ImmutableList <String > path ,
35+ ImmutableMap <String , String > queryParams ) {
2836 this .channelProvider = channelProvider ;
37+ this .path = path ;
38+ this .queryParams = queryParams ;
2939 }
3040
3141 public WebTarget path (String ... components ) {
42+ ImmutableList .Builder <String > newPath = ImmutableList .<String >builder ().addAll (this .path );
3243
3344 for (String component : components ) {
34-
35- path .addAll (Arrays .asList (StringUtils .split (component , PATH_SEPARATOR )));
45+ newPath .addAll (Arrays .asList (StringUtils .split (component , PATH_SEPARATOR )));
3646 }
3747
38- return this ;
48+ return new WebTarget ( channelProvider , newPath . build (), queryParams ) ;
3949 }
4050
4151 public InvocationBuilder request () {
4252 String resource = PATH_SEPARATOR + StringUtils .join (path , PATH_SEPARATOR );
4353
44- List <String > params = new ArrayList <String >();
54+ List <String > params = new ArrayList <>();
4555 for (Map .Entry <String , String > entry : queryParams .entrySet ()) {
4656 params .add (entry .getKey () + "=" + entry .getValue ());
4757 }
@@ -54,20 +64,47 @@ public InvocationBuilder request() {
5464 }
5565
5666 public WebTarget resolveTemplate (String name , Object value ) {
57- List <String > newPath = new ArrayList < String > ();
67+ ImmutableList . Builder <String > newPath = ImmutableList . builder ();
5868 for (String component : path ) {
5969 component = component .replaceAll ("\\ {" + name + "\\ }" , value .toString ());
6070 newPath .add (component );
6171 }
62- path = newPath ;
63- return this ;
72+ return new WebTarget (channelProvider , newPath .build (), queryParams );
6473 }
6574
6675 public WebTarget queryParam (String name , Object value ) {
76+ ImmutableMap .Builder <String , String > builder = ImmutableMap .<String , String >builder ().putAll (queryParams );
6777 if (value != null ) {
68- queryParams .put (name , value .toString ());
78+ builder .put (name , value .toString ());
6979 }
70- return this ;
80+ return new WebTarget ( channelProvider , path , builder . build ()) ;
7181 }
7282
83+ @ Override
84+ public boolean equals (Object o ) {
85+ if (this == o ) {
86+ return true ;
87+ }
88+ if (o == null || getClass () != o .getClass ()) {
89+ return false ;
90+ }
91+
92+ WebTarget webTarget = (WebTarget ) o ;
93+
94+ if (channelProvider != null ? !channelProvider .equals (webTarget .channelProvider ) : webTarget .channelProvider != null ) {
95+ return false ;
96+ }
97+ if (path != null ? !path .equals (webTarget .path ) : webTarget .path != null ) {
98+ return false ;
99+ }
100+ return queryParams != null ? queryParams .equals (webTarget .queryParams ) : webTarget .queryParams == null ;
101+ }
102+
103+ @ Override
104+ public int hashCode () {
105+ int result = channelProvider != null ? channelProvider .hashCode () : 0 ;
106+ result = 31 * result + (path != null ? path .hashCode () : 0 );
107+ result = 31 * result + (queryParams != null ? queryParams .hashCode () : 0 );
108+ return result ;
109+ }
73110}
0 commit comments