Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added IgnoreDefaultEqualsAndToString #566

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/main/groovy/lang/IgnoreDefaultEqualsAndToString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package groovy.lang;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Classes marked with this {@link IgnoreDefaultEqualsAndToString} annotation
* by-pass the default groovy formatting and equality rules and allowing
* a user to provide a custom format and equals method
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface IgnoreDefaultEqualsAndToString {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12026,6 +12026,9 @@ public static boolean equals(List left, List right) {
if (left == right) {
return true;
}
if( left.getClass().getAnnotation(IgnoreDefaultEqualsAndToString.class)!=null && right.getClass().getAnnotation(IgnoreDefaultEqualsAndToString.class)!=null ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1 to this until we have a better idea on the impact on performance. Calling getClass().getAnnotation(...) is very expensive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you suggest a possible metrics/benchmark ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have nothing particular in mind at this point, but it involves this method being called with different kind of objects, and comparing with the baseline without the change.

return left.equals(right);
}
if (left.size() != right.size()) {
return false;
}
Expand Down Expand Up @@ -12078,6 +12081,9 @@ public static <T> boolean equals(Set<T> self, Set<T> other) {
if (self == other) {
return true;
}
if( self.getClass().getAnnotation(IgnoreDefaultEqualsAndToString.class)!=null && other.getClass().getAnnotation(IgnoreDefaultEqualsAndToString.class)!=null ) {
return self.equals(other);
}
if (self.size() != other.size()) {
return false;
}
Expand Down Expand Up @@ -12122,6 +12128,9 @@ public static boolean equals(Map self, Map other) {
if (self == other) {
return true;
}
if( self.getClass().getAnnotation(IgnoreDefaultEqualsAndToString.class)!=null && other.getClass().getAnnotation(IgnoreDefaultEqualsAndToString.class)!=null ) {
return self.equals(other);
}
if (self.size() != other.size()) {
return false;
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/org/codehaus/groovy/runtime/InvokerHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import groovy.lang.GroovyObject;
import groovy.lang.GroovyRuntimeException;
import groovy.lang.GroovySystem;
import groovy.lang.IgnoreDefaultEqualsAndToString;
import groovy.lang.MetaClass;
import groovy.lang.MetaClassRegistry;
import groovy.lang.MissingMethodException;
Expand Down Expand Up @@ -685,6 +686,9 @@ private static String handleFormattingException(Object item, Exception ex) {
}

private static String formatMap(Map map, boolean verbose, int maxSize, boolean safe) {
if (map.getClass().getAnnotation(IgnoreDefaultEqualsAndToString.class)!=null) {
return map.toString();
}
if (map.isEmpty()) {
return "[:]";
}
Expand Down Expand Up @@ -723,6 +727,9 @@ private static int sizeLeft(int maxSize, StringBuilder buffer) {
}

private static String formatCollection(Collection collection, boolean verbose, int maxSize, boolean safe) {
if (collection.getClass().getAnnotation(IgnoreDefaultEqualsAndToString.class)!=null) {
return collection.toString();
}
StringBuilder buffer = new StringBuilder(ITEM_ALLOCATE_SIZE * collection.size());
buffer.append('[');
boolean first = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,56 @@ public class DefaultGroovyMethodsTest extends GroovyTestCase {
delegate.iterator()
}
}


@IgnoreDefaultEqualsAndToString
private static class CustomList extends ArrayList {
CustomList( Object... items) {
addAll(items)
}

@Override
boolean equals(Object other) {
return this.sum() == other.sum()
}
}

@IgnoreDefaultEqualsAndToString
private static class CustomSet extends HashSet {
CustomSet( Object... items) {
addAll(items)
}

@Override
boolean equals(Object other) {
return this.collect { it.toString().toUpperCase() } == other.collect { it.toString().toUpperCase() }
}
}

@IgnoreDefaultEqualsAndToString
private static class CustomMap extends HashMap {
CustomMap(Map params) {
this.putAll(params)
}

boolean equals(Object other) {
this.values().sum() == other.values().sum()
}
}

public void testCustomEqualsForList() {

assertTrue(new CustomList(1,2,3).equals(new CustomList(3,3)))
assertTrue(new CustomList(1,2,3) == new CustomList(3,3) )

assertTrue(new CustomSet('a','b','c').equals(new CustomSet('A','B','C')))
assertTrue(new CustomSet('a','b','c') == new CustomSet('A','B','C') )

assertTrue(new CustomMap(a:10) == new CustomMap(b:3,c:7))
assertTrue(new CustomMap(a:10).equals(new CustomMap(b:3,c:7)))
assertFalse(new CustomMap(a:1) == new CustomMap(b:2))

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,44 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
assert '[(this Map):(this Map)]' == InvokerHelper.toString(m)
}



public void testIgnoreDefaultToStringForCustomList() {

String TEST = '''
@IgnoreDefaultEqualsAndToString
class Foo extends ArrayList {
String toString() { return this.join('-') }
}

def foo = new Foo()
foo << 1 << 2 << 3
assert foo.toString() == "1-2-3"
assert "$foo".toString() == "1-2-3"
return true
'''

assertTrue((Boolean)Eval.me(TEST));

}

public void testIgnoreDefaultToStringForCustomMap() {

String TEST = '''
@IgnoreDefaultEqualsAndToString
class Foo extends HashMap {
String toString() { return 'MyCustomToString' }
}

def foo = new Foo()
assert foo.toString() == 'MyCustomToString'
assert "$foo".toString() == 'MyCustomToString'
return true
'''

assertTrue((Boolean)Eval.me(TEST));

}


}