You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "cda (Jira)" <ji...@apache.org> on 2020/02/22 22:51:00 UTC
[jira] [Comment Edited] (HTTPCLIENT-2025) URIBuilder clone
[ https://issues.apache.org/jira/browse/HTTPCLIENT-2025?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17042736#comment-17042736 ]
cda edited comment on HTTPCLIENT-2025 at 2/22/20 10:50 PM:
-----------------------------------------------------------
[~olegk] identity equality is done using "==" operator. Equals method is usually overriden to do equality by value (by default it only does identity equality).
For example *java.lang.String*'s equals method does equality by value:
{code:java}
new String("asd").equals( new String("asd") ) //true: different objects but same value
new String("asd") == new String("asd") //false: not same object, different identity
{code}
Same for hashcode:
{code:java}
new String("asd").hashCode() == new String("asd").hashCode() //true: different objects but same hash
System.identityHashCode( new String("asd") ) == System.identityHashCode( new String("asd").hashCode() ) //false: different objects with different identity hash
{code}
I want the same behaviour for *URIBuilder.*
Here is an example of what I'm trying to do. Commented lines are code that I currently use.
{code:java}
public class URLImpl {
private URIBuilder url;
@Override public boolean equals( Object o ) {
if (this == o) return true;
if (!(o instanceof URLImpl))
return false;
// try {
// return url.build().normalize().toString().equals(
// ((URLImpl) o).url.build().normalize().toString() );
// } catch (URISyntaxException e) {
// return false;
// }
URLImpl other = (URLImpl) o;
return url.equals( other.url );
}
@Override public int hashCode() {
// try {
// return url.build().normalize().toString().hashCode();
// } catch (URISyntaxException e) {
// throw new RuntimeException( e );
// }
return url.hashCode();
}
}
{code}
was (Author: cdalxndr):
[~olegk] identity equality is done using "==" operator. Equals method is usually overwritten to do equality by value (by default it only does identity equality).
For example *java.lang.String*'s equals method does equality by value:
{code:java}
new String("asd").equals( new String("asd") ) //true: different objects but same value
new String("asd") == new String("asd") //false: not same object, different identity
{code}
Same for hashcode:
{code:java}
new String("asd").hashCode() == new String("asd").hashCode() //true: different objects but same hash
System.identityHashCode( new String("asd") ) == System.identityHashCode( new String("asd").hashCode() ) //false: different objects with different identity hash
{code}
I want the same behaviour for *URIBuilder.*
Here is an example of what I'm trying to do. Commented lines are code that I currently use.
{code:java}
public class URLImpl {
private URIBuilder url;
@Override public boolean equals( Object o ) {
if (this == o) return true;
if (!(o instanceof URLImpl))
return false;
// try {
// return url.build().normalize().toString().equals(
// ((URLImpl) o).url.build().normalize().toString() );
// } catch (URISyntaxException e) {
// return false;
// }
URLImpl other = (URLImpl) o;
return url.equals( other.url );
}
@Override public int hashCode() {
// try {
// return url.build().normalize().toString().hashCode();
// } catch (URISyntaxException e) {
// throw new RuntimeException( e );
// }
return url.hashCode();
}
}
{code}
> URIBuilder clone
> ----------------
>
> Key: HTTPCLIENT-2025
> URL: https://issues.apache.org/jira/browse/HTTPCLIENT-2025
> Project: HttpComponents HttpClient
> Issue Type: Improvement
> Affects Versions: 4.5.10
> Reporter: cda
> Priority: Minor
>
> Please provide a method to clone an *URIBuilder* instance using *Cloneable* interface or a copy constructor.
> *Use case*: need to modify and return a new instance without altering current instance. Currently, I have to use *new URIBuilder(uriBuilder.toString())* which is relative expensive compared to cloning fields.
> + need to implement *equals* and *hashCode*
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org