1 | package com.github.dakusui.pcond.core.identifieable; | |
2 | ||
3 | import java.util.List; | |
4 | import java.util.Objects; | |
5 | import java.util.Optional; | |
6 | import java.util.stream.Stream; | |
7 | ||
8 | import static java.util.Collections.singletonList; | |
9 | import static java.util.stream.Collectors.toList; | |
10 | ||
11 | /** | |
12 | * An interface that represents an object which can be identified based on objects | |
13 | * used on instantiation without overriding the {@link Object#hashCode} and {@link Object#equals(Object)}. | |
14 | * In order to minimize effort to implement this interface, you can extend | |
15 | * {@link Identifiable.Base} class. | |
16 | */ | |
17 | public interface Identifiable { | |
18 | static Optional<Object> creatorOf(Object object) { | |
19 | Optional<Object> ret = Optional.empty(); | |
20 |
1
1. creatorOf : negated conditional → KILLED |
if (object instanceof Identifiable) |
21 | ret = Optional.of(((Identifiable) object).creator()); | |
22 |
1
1. creatorOf : replaced return value with Optional.empty for com/github/dakusui/pcond/core/identifieable/Identifiable::creatorOf → KILLED |
return ret; |
23 | } | |
24 | ||
25 | static List<Object> argsOf(Object object) { | |
26 | List<Object> ret = singletonList(object); | |
27 |
1
1. argsOf : negated conditional → KILLED |
if (object instanceof Identifiable) |
28 | ret = ((Identifiable) object).args(); | |
29 |
1
1. argsOf : replaced return value with Collections.emptyList for com/github/dakusui/pcond/core/identifieable/Identifiable::argsOf → KILLED |
return ret; |
30 | } | |
31 | ||
32 | static <T> String formatObjectName(Object object) { | |
33 |
2
1. formatObjectName : negated conditional → SURVIVED 2. formatObjectName : replaced return value with "" for com/github/dakusui/pcond/core/identifieable/Identifiable::formatObjectName → SURVIVED |
return "noname:" + (object == null ? "null" : object.toString()); |
34 | } | |
35 | ||
36 | Object creator(); | |
37 | ||
38 | /** | |
39 | * This method is designed to be called from the {@code hashCode()} method of a class | |
40 | * which implements {@link Identifiable} identity interface. | |
41 | * | |
42 | * @return A proper value to be returned from the {@link Object#hashCode()} method. | |
43 | */ | |
44 | default int defaultHashCode() { | |
45 |
1
1. defaultHashCode : replaced int return with 0 for com/github/dakusui/pcond/core/identifieable/Identifiable::defaultHashCode → KILLED |
return identityObject().hashCode(); |
46 | } | |
47 | ||
48 | /** | |
49 | * This method is designed to be called from the {@code defaultEquals()} method of a class | |
50 | * which implements {@link Identifiable} identity interface. | |
51 | * | |
52 | * @return A proper value to be returned from the {@link Object#equals(Object)} method. | |
53 | */ | |
54 | default boolean defaultEquals(Object anotherObject) { | |
55 |
1
1. defaultEquals : negated conditional → KILLED |
if (this == anotherObject) |
56 |
1
1. defaultEquals : replaced boolean return with false for com/github/dakusui/pcond/core/identifieable/Identifiable::defaultEquals → KILLED |
return true; |
57 |
1
1. defaultEquals : negated conditional → KILLED |
if (!this.getClass().isInstance(anotherObject)) |
58 |
1
1. defaultEquals : replaced boolean return with true for com/github/dakusui/pcond/core/identifieable/Identifiable::defaultEquals → KILLED |
return false; |
59 |
2
1. defaultEquals : replaced boolean return with false for com/github/dakusui/pcond/core/identifieable/Identifiable::defaultEquals → KILLED 2. defaultEquals : replaced boolean return with true for com/github/dakusui/pcond/core/identifieable/Identifiable::defaultEquals → KILLED |
return Objects.equals(this.identityObject(), ((Identifiable) anotherObject).identityObject()); |
60 | } | |
61 | ||
62 | default Object createIdentity() { | |
63 |
1
1. createIdentity : replaced return value with null for com/github/dakusui/pcond/core/identifieable/Identifiable::createIdentity → KILLED |
return Stream.concat( |
64 | Stream.of(creator()), | |
65 | args().stream()).collect(toList()); | |
66 | } | |
67 | ||
68 | /** | |
69 | * Typically, implementation of this method should return a final field value to which the | |
70 | * value returned by {@link Identifiable#createIdentity()} method is assigned in the constructor. | |
71 | * | |
72 | * @return The identity object. | |
73 | */ | |
74 | Object identityObject(); | |
75 | ||
76 | List<Object> args(); | |
77 | ||
78 | class Base implements Identifiable { | |
79 | private final Object creator; | |
80 | private final List<Object> args; | |
81 | private final Object identity; | |
82 | ||
83 | protected Base(Object creator, List<Object> args) { | |
84 | this.creator = Objects.requireNonNull(creator); | |
85 | this.args = Objects.requireNonNull(args); | |
86 | this.identity = createIdentity(); | |
87 | } | |
88 | ||
89 | @Override | |
90 | public Object identityObject() { | |
91 |
1
1. identityObject : replaced return value with null for com/github/dakusui/pcond/core/identifieable/Identifiable$Base::identityObject → KILLED |
return this.identity; |
92 | } | |
93 | ||
94 | @Override | |
95 | public Object creator() { | |
96 |
1
1. creator : replaced return value with null for com/github/dakusui/pcond/core/identifieable/Identifiable$Base::creator → KILLED |
return this.creator; |
97 | } | |
98 | ||
99 | @Override | |
100 | public List<Object> args() { | |
101 |
1
1. args : replaced return value with Collections.emptyList for com/github/dakusui/pcond/core/identifieable/Identifiable$Base::args → KILLED |
return args; |
102 | } | |
103 | ||
104 | @Override | |
105 | public int hashCode() { | |
106 |
1
1. hashCode : replaced int return with 0 for com/github/dakusui/pcond/core/identifieable/Identifiable$Base::hashCode → KILLED |
return defaultHashCode(); |
107 | } | |
108 | ||
109 | // Done in a method to which the operation is delegated. | |
110 | @SuppressWarnings("EqualsWhichDoesntCheckParameterClass") | |
111 | @Override | |
112 | public boolean equals(Object obj) { | |
113 |
2
1. equals : replaced boolean return with false for com/github/dakusui/pcond/core/identifieable/Identifiable$Base::equals → KILLED 2. equals : replaced boolean return with true for com/github/dakusui/pcond/core/identifieable/Identifiable$Base::equals → KILLED |
return defaultEquals(obj); |
114 | } | |
115 | } | |
116 | } | |
Mutations | ||
20 |
1.1 |
|
22 |
1.1 |
|
27 |
1.1 |
|
29 |
1.1 |
|
33 |
1.1 2.2 |
|
45 |
1.1 |
|
55 |
1.1 |
|
56 |
1.1 |
|
57 |
1.1 |
|
58 |
1.1 |
|
59 |
1.1 2.2 |
|
63 |
1.1 |
|
91 |
1.1 |
|
96 |
1.1 |
|
101 |
1.1 |
|
106 |
1.1 |
|
113 |
1.1 2.2 |