1 | /* |
2 | * JTiger Unit Testing Framework for J2SE 1.5 |
3 | * Copyright (C) 2005 Tony Morris |
4 | * |
5 | * This software is licenced under the |
6 | * Common Public Licence version 1.0 |
7 | * http://www.opensource.org/licenses/cpl1.0.php |
8 | * |
9 | * You received a copy of this licence with this software. |
10 | */ |
11 | package org.jtiger.assertion; |
12 | |
13 | final class HashCodeMethodContractTesterFactory |
14 | { |
15 | private HashCodeMethodContractTesterFactory() |
16 | { |
17 | |
18 | } |
19 | |
20 | static HashCodeMethodContractTester newHashCodeMethodContractTester(final ObjectFactory<?> factory) throws NullPointerException |
21 | { |
22 | if(factory == null) |
23 | { |
24 | throw new NullPointerException(); |
25 | } |
26 | |
27 | return new HashCodeMethodContractTesterImpl(factory); |
28 | } |
29 | |
30 | private static final class HashCodeMethodContractTesterImpl implements HashCodeMethodContractTester |
31 | { |
32 | private final ObjectFactory<?> factory; |
33 | |
34 | HashCodeMethodContractTesterImpl(final ObjectFactory<?> factory) |
35 | { |
36 | this.factory = factory; |
37 | } |
38 | |
39 | public boolean isConsistentResult() |
40 | { |
41 | for(int i = 0; i < 20; i++) |
42 | { |
43 | final Object x = factory.newInstanceX(); |
44 | final Object y = factory.newInstanceY(); |
45 | |
46 | if(!(x.hashCode() == x.hashCode() && y.hashCode() == y.hashCode())) |
47 | { |
48 | return false; |
49 | } |
50 | } |
51 | |
52 | return true; |
53 | } |
54 | |
55 | public boolean isEqualOnEqualInstance() |
56 | { |
57 | final Object x1 = factory.newInstanceX(); |
58 | final Object x2 = factory.newInstanceX(); |
59 | final Object y1 = factory.newInstanceY(); |
60 | final Object y2 = factory.newInstanceY(); |
61 | |
62 | return x1.hashCode() == x2.hashCode() && y1.hashCode() == y2.hashCode(); |
63 | } |
64 | |
65 | public boolean fillsContract() |
66 | { |
67 | return isConsistentResult() && isEqualOnEqualInstance(); |
68 | } |
69 | } |
70 | } |