View Javadoc

1   /*
2    * Copyright (c) 2007, James Leigh All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this
8    *   list of conditions and the following disclaimer.
9    * - Redistributions in binary form must reproduce the above copyright notice,
10   *   this list of conditions and the following disclaimer in the documentation
11   *   and/or other materials provided with the distribution. 
12   * - Neither the name of the openrdf.org nor the names of its contributors may
13   *   be used to endorse or promote products derived from this software without
14   *   specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   * 
28   */
29  package org.openrdf.elmo.sesame.helpers;
30  
31  import java.util.Set;
32  
33  import org.openrdf.model.Resource;
34  import org.openrdf.model.URI;
35  import org.openrdf.model.Value;
36  import org.openrdf.repository.RepositoryException;
37  import org.openrdf.repository.contextaware.ContextAwareConnection;
38  
39  /**
40   * Performs the adding and removing of statements from the repository.
41   * 
42   * @author James Leigh
43   * 
44   */
45  public class PropertyChanger {
46  	private URI pred;
47  
48  	private Set<? extends Value> oneOf;
49  
50  	public PropertyChanger(URI pred) {
51  		assert pred != null;
52  		this.pred = pred;
53  	}
54  
55  	public PropertyChanger(URI pred, Set<? extends Value> oneOf) {
56  		this.pred = pred;
57  		this.oneOf = oneOf;
58  	}
59  
60  	public URI getPredicate() {
61  		return pred;
62  	}
63  
64  	public void add(ContextAwareConnection conn, Resource subj, Value obj)
65  			throws RepositoryException {
66  		assert oneOf == null || oneOf.contains(obj) || oneOf.contains(subj) : oneOf;
67  		conn.add(subj, pred, obj);
68  	}
69  
70  	public void remove(ContextAwareConnection conn, Resource subj, Value obj)
71  			throws RepositoryException {
72  		conn.remove(subj, pred, obj);
73  	}
74  
75  	@Override
76  	public String toString() {
77  		return pred.toString();
78  	}
79  
80  	@Override
81  	public int hashCode() {
82  		final int PRIME = 31;
83  		int result = 1;
84  		result = PRIME * result + ((pred == null) ? 0 : pred.hashCode());
85  		return result;
86  	}
87  
88  	@Override
89  	public boolean equals(Object obj) {
90  		if (this == obj)
91  			return true;
92  		if (obj == null)
93  			return false;
94  		if (getClass() != obj.getClass())
95  			return false;
96  		final PropertyChanger other = (PropertyChanger) obj;
97  		if (pred == null) {
98  			if (other.pred != null)
99  				return false;
100 		} else if (!pred.equals(other.pred))
101 			return false;
102 		return true;
103 	}
104 }