View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    *  http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Alternatively, the contents of this file may be used under the terms of
10   * either the GNU Lesser General Public License Version 2.1 or later (the
11   * "LGPL"), in which case the provisions of the LGPL are applicable instead
12   * of those above. If you wish to allow use of your version of this file only
13   * under the terms of the LGPL, and not to allow others to use your version of
14   * this file under the terms of the EPL, indicate your decision by deleting
15   * the provisions above and replace them with the notice and other provisions
16   * required by the LGPL. If you do not delete the provisions above, a recipient
17   * may use your version of this file under the terms of the EPL or the LGPL.
18   *
19   * Based on the original MiniSat specification from:
20   *
21   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22   * Sixth International Conference on Theory and Applications of Satisfiability
23   * Testing, LNCS 2919, pp 502-518, 2003.
24   *
25   * See www.minisat.se for the original solver in C++.
26   *
27   * Contributors:
28   *   CRIL - initial API and implementation
29   *******************************************************************************/
30  
31  package org.sat4j.pb.tools;
32  
33  import java.math.BigInteger;
34  
35  public final class WeightedObject<T> implements Comparable<WeightedObject<T>> {
36  
37  	public final T thing;
38  	private BigInteger weight;
39  
40  	private WeightedObject(T thing, BigInteger weight) {
41  		this.thing = thing;
42  		this.weight = weight;
43  	}
44  
45  	public BigInteger getWeight() {
46  		return this.weight;
47  	}
48  
49  	public void increaseWeight(BigInteger delta) {
50  		this.weight = this.weight.add(delta);
51  	}
52  
53  	public int compareTo(WeightedObject<T> arg0) {
54  		return this.weight.compareTo(arg0.getWeight());
55  	}
56  
57  	public static <E> WeightedObject<E> newWO(E e, int w) {
58  		return new WeightedObject<E>(e, BigInteger.valueOf(w));
59  	}
60  
61  	public static <E> WeightedObject<E> newWO(E e, long w) {
62  		return new WeightedObject<E>(e, BigInteger.valueOf(w));
63  	}
64  
65  	public static <E> WeightedObject<E> newWO(E e, BigInteger w) {
66  		return new WeightedObject<E>(e, w);
67  	}
68  
69  	@Override
70  	public int hashCode() {
71  		final int prime = 31;
72  		int result = 1;
73  		result = prime * result
74  				+ (this.thing == null ? 0 : this.thing.hashCode());
75  		result = prime * result
76  				+ (this.weight == null ? 0 : this.weight.hashCode());
77  		return result;
78  	}
79  
80  	@Override
81  	public boolean equals(Object obj) {
82  		if (this == obj) {
83  			return true;
84  		}
85  		if (obj == null) {
86  			return false;
87  		}
88  		if (getClass() != obj.getClass()) {
89  			return false;
90  		}
91  		WeightedObject<?> other = (WeightedObject<?>) obj;
92  		if (this.thing == null) {
93  			if (other.thing != null) {
94  				return false;
95  			}
96  		} else if (!this.thing.equals(other.thing)) {
97  			return false;
98  		}
99  		if (this.weight == null) {
100 			if (other.weight != null) {
101 				return false;
102 			}
103 		} else if (!this.weight.equals(other.weight)) {
104 			return false;
105 		}
106 		return true;
107 	}
108 }