You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by sf...@apache.org on 2010/12/12 16:02:37 UTC
svn commit: r1044829 [5/12] - in /incubator/stanbol/trunk/rick/generic:
core/src/main/java/eu/iksproject/rick/core/impl/
core/src/main/java/eu/iksproject/rick/core/mapping/
core/src/main/java/eu/iksproject/rick/core/model/
core/src/main/java/eu/iksproj...
Modified: incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/model/InMemoryRepresentation.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/model/InMemoryRepresentation.java?rev=1044829&r1=1044828&r2=1044829&view=diff
==============================================================================
--- incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/model/InMemoryRepresentation.java (original)
+++ incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/model/InMemoryRepresentation.java Sun Dec 12 15:02:34 2010
@@ -23,331 +23,331 @@ import eu.iksproject.rick.servicesapi.mo
import eu.iksproject.rick.servicesapi.model.ValueFactory;
public class InMemoryRepresentation implements Representation,Cloneable {
-
- //private final Logger log = LoggerFactory.getLogger(InMemoryRepresentation.class);
- private static ValueFactory valueFactory = new InMemoryValueFactory();
-
- protected final Map<String,Object> representation;
- protected final Map<String,Object> unmodRepresentation;
- private final String id;
-
- public InMemoryRepresentation(String id){
- this(id,null);
- }
- /**
- * Initialise a new InMemoryRepresenation that contains already some data.
- * @param id
- * @param representation
- */
- protected InMemoryRepresentation(String id, Map<String,Object> representation){
- if(id == null){
- throw new IllegalArgumentException("The id of a Representation instance MUST NOT be NULL!");
- }
- this.id = id;
- if(representation == null){
- this.representation = new HashMap<String, Object>();
- } else {
- this.representation = representation;
- }
- unmodRepresentation = Collections.unmodifiableMap(this.representation);
- }
- @SuppressWarnings("unchecked")
- @Override
- public void add(String field, Object parsedValue) {
- //TODO:add processing of values
- // URI, URL -> Reference
- // String[] -> Text
- // check Collections!
- // The rest should be added as Objects
- Collection<Object> newValues = new ArrayList<Object>();
- ModelUtils.checkValues(valueFactory, parsedValue, newValues);
- Object values = representation.get(field);
- if(values != null){
- if(values instanceof Collection<?>){
- ((Collection<Object>) values).addAll(newValues);
- } else {
- if(newValues.size() == 1 && values.equals(newValues.iterator().next())){
- return; //do not create an collection of the current value equals the added
- }
- Collection<Object> collection = new HashSet<Object>();
- //reset the field to the collection
- representation.put(field, collection);
- //add the two values
- collection.add(values);
- collection.addAll(newValues);
- }
- } else {
- //also here do not add the collection if there is only one value!
- representation.put(field, newValues.size() == 1?newValues.iterator().next():newValues);
- }
- }
- protected void addValues(String field,Collection<Object> values){
-
- }
-
- @Override
- public void addNaturalText(String field, String text, String... languages) {
- if(languages == null || languages.length<1){ //if no language is parse add the default lanugage!
- add(field,valueFactory.createText(text, null));
- } else {
- for(String lang : languages){
- add(field,valueFactory.createText(text, lang));
- }
- }
- }
-
- @Override
- public void addReference(String field, String reference) {
- add(field, valueFactory.createReference(reference));
- }
- /**
- * Getter for the values of the field as Collections. If the field is not
- * present it returns an empty Collections!
- * @param field the field
- * @return A read only collection with the values of the field
- */
- @SuppressWarnings("unchecked")
- private Collection<Object> getValuesAsCollection(String field){
- Object value = representation.get(field);
- if(value == null){
- return Collections.emptySet();
- } else if(value instanceof Collection<?>){
- return (Collection<Object>)value;
- } else {
- return Collections.singleton(value);
- }
- }
- @Override
- public <T> Iterator<T> get(String field, Class<T> type) throws UnsupportedTypeException {
- Collection<Object> values = getValuesAsCollection(field);
- return new TypeSaveIterator<T>(values.iterator(), type);
- }
-
- @Override
- public Iterator<Object> get(String field) {
- return getValuesAsCollection(field).iterator();
- }
- @Override
- public Iterator<Text> getText(String field) {
- Collection<Object> values = getValuesAsCollection(field);
- return values != null?new TextIterator(valueFactory,values.iterator()):null;
- }
-
- @Override
- public Iterator<Text> get(String field, String... languages) {
- final Collection<Object> values = getValuesAsCollection(field);
- return new TextIterator(valueFactory,values.iterator(), languages);
- }
-
- @Override
- public Iterator<String> getFieldNames() {
- return unmodRepresentation.keySet().iterator();
- }
-
- @Override
- public <T> T getFirst(String field, Class<T> type) throws UnsupportedTypeException {
- Iterator<T> values = get(field,type);
- return values.hasNext()?values.next():null;
- }
-
- @Override
- public Object getFirst(String field) {
- Iterator<Object> values = get(field);
- return values.hasNext()?values.next():null;
- }
-
- @Override
- public Text getFirst(String field, String... languages) {
- Iterator<Text> values = get(field,languages);
- return values.hasNext()?values.next():null;
- }
-
- @Override
- public String getId() {
- return id;
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public void remove(String field, Object value) {
- Object values = representation.get(field);
- if(values == null) return;
- if(value.equals(value)){
- representation.remove(field);
- } else if(values instanceof Collection<?>){
- if(((Collection<Object>)values).remove(value) && //remove the Element
- ((Collection<Object>)values).size()<2){ //if removed check for size
- //it only one element remaining -> replace the collection with a Object
- representation.put(field, ((Collection<Object>)values).iterator().next());
- }
-
- } //else ignore
- }
-
- @Override
- public void removeAll(String field) {
- representation.remove(field);
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public void removeAllNaturalText(String field, String... languages) {
- Object values = representation.get(field);
- if(values == null) return;
- if(values instanceof Collection<?>){
- int removed = 0;
- for(Iterator<Text> it = new TextIterator(valueFactory,
- ((Collection<Object>)values).iterator(),
- languages);it.hasNext();){
- it.next();//go to the next Element
- it.remove(); //and remove ist
- removed++;
- }
- if(removed>0){ //if some elements where removed
- //check if there is only a singe or no elements left for the field
- int size = ((Collection<Object>)values).size();
- if(size==1){
- representation.put(field, ((Collection<Object>)values).iterator().next());
- } else if(size<1){
- representation.remove(field);
- }
- }
- } else if(isNaturalLanguageValue(values, languages)){
- representation.remove(field);
- } //else there is a single value that does not fit -> nothing todo
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public void removeNaturalText(String field, String text, String... languages) {
- Object values = representation.get(field);
- if(values == null) return;
- if(values instanceof Collection<?>){
- int removed = 0;
- for(Iterator<Text> it = new TextIterator(valueFactory,
- ((Collection<Object>)values).iterator(),
- languages);it.hasNext();){
- Text label = it.next();//go to the next element
- if(text.equals(label.getText())){
- it.remove();//and remove it
- removed++;
- }
- }
- if(removed>0){ //if some elements where removed
- //check if there is only a singe or no elements left for the field
- int size = ((Collection<Object>)values).size();
- if(size==1){
- representation.put(field, ((Collection<Object>)values).iterator().next());
- } else if(size<1){
- representation.remove(field);
- }
- }
- } else if(text.equals(getNaturalLanguageValue(values, languages))){
- representation.remove(field);
- } //else there is a single value that does not fit -> nothing todo
-
- }
-
- @Override
- public void removeReference(String field, String reference) {
- try {
- remove(field,new URI(reference));
- } catch (URISyntaxException e) {
- throw new IllegalArgumentException("parsed reference needs to be an valid URI",e);
- }
- }
-
- @Override
- public void set(String field, Object value) {
- representation.remove(field);
- add(field,value);
-
- }
-
- @Override
- public void setNaturalText(String field, String text, String... languages) {
- removeAllNaturalText(field, languages);
- if(text != null){
- addNaturalText(field, text, languages);
- }
- }
-
- @Override
- public void setReference(String field, String reference) {
- removeAll(field);
- if(reference != null){
- addReference(field, reference);
- }
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public Object clone() throws CloneNotSupportedException {
- Map<String,Object> clone = new HashMap<String, Object>();
- for(Entry<String,Object> e : representation.entrySet()){
- if(e.getValue() instanceof HashSet<?>){
- clone.put(e.getKey(), ((HashSet<?>)e.getValue()).clone());
- } else if(e.getValue() instanceof Collection<?>){
- HashSet<Object> valuesClone = new HashSet<Object>();
- for(Iterator<Object> it = ((Collection<Object>)e.getValue()).iterator();it.hasNext();valuesClone.add(it.next()));
- clone.put(e.getKey(), valuesClone);
- } else {
- clone.put(e.getKey(), e.getValue());
- }
- }
- return new InMemoryRepresentation(id, clone);
- }
- @Override
- public Reference getFirstReference(String field) {
- Iterator<Reference> it = getReferences(field);
- return it.hasNext()?it.next():null;
- }
- @Override
- public Iterator<Reference> getReferences(String field) {
- Collection<Object> values = getValuesAsCollection(field);
- return new TypeSaveIterator<Reference>(values.iterator(), Reference.class);
- }
- private static String getNaturalLanguageValue(Object check,Set<String> langSet,boolean isNullLanguage){
- if(check instanceof Text){
- Text text = (Text)check;
- if(langSet == null || langSet.contains(text.getLanguage())){
- return text.getText();
- } // else empty arrey -> filter
- } else if(isNullLanguage && check instanceof String){
- return (String)check;
- } //type does not fit -> ignore
- return null; //no label found
- }
- public static String getNaturalLanguageValue(Object check,String...languages){
- Set<String> langSet;
- boolean isNullLanguage;
- if(languages != null && languages.length>1){
- langSet = new HashSet<String>(Arrays.asList(languages));
- isNullLanguage = langSet.contains(null);
- } else {
- langSet = null;
- isNullLanguage = true;
- }
- return getNaturalLanguageValue(check,langSet,isNullLanguage);
- }
- /**
- * @param check
- * @param languages
- * @return
- */
- public static boolean isNaturalLanguageValue(Object check,String...languages){
- return getNaturalLanguageValue(check,languages) != null;
- }
- @Override
- public String toString() {
- return InMemoryRepresentation.class.getSimpleName()+getId();
- }
- @Override
- public int hashCode() {
- return getId().hashCode();
- }
- @Override
- public boolean equals(Object obj) {
- return obj != null && obj instanceof Representation && ((Representation)obj).getId().equals(getId());
- }
+
+ //private final Logger log = LoggerFactory.getLogger(InMemoryRepresentation.class);
+ private static ValueFactory valueFactory = new InMemoryValueFactory();
+
+ protected final Map<String,Object> representation;
+ protected final Map<String,Object> unmodRepresentation;
+ private final String id;
+
+ public InMemoryRepresentation(String id){
+ this(id,null);
+ }
+ /**
+ * Initialise a new InMemoryRepresenation that contains already some data.
+ * @param id
+ * @param representation
+ */
+ protected InMemoryRepresentation(String id, Map<String,Object> representation){
+ if(id == null){
+ throw new IllegalArgumentException("The id of a Representation instance MUST NOT be NULL!");
+ }
+ this.id = id;
+ if(representation == null){
+ this.representation = new HashMap<String, Object>();
+ } else {
+ this.representation = representation;
+ }
+ unmodRepresentation = Collections.unmodifiableMap(this.representation);
+ }
+ @SuppressWarnings("unchecked")
+ @Override
+ public void add(String field, Object parsedValue) {
+ //TODO:add processing of values
+ // URI, URL -> Reference
+ // String[] -> Text
+ // check Collections!
+ // The rest should be added as Objects
+ Collection<Object> newValues = new ArrayList<Object>();
+ ModelUtils.checkValues(valueFactory, parsedValue, newValues);
+ Object values = representation.get(field);
+ if(values != null){
+ if(values instanceof Collection<?>){
+ ((Collection<Object>) values).addAll(newValues);
+ } else {
+ if(newValues.size() == 1 && values.equals(newValues.iterator().next())){
+ return; //do not create an collection of the current value equals the added
+ }
+ Collection<Object> collection = new HashSet<Object>();
+ //reset the field to the collection
+ representation.put(field, collection);
+ //add the two values
+ collection.add(values);
+ collection.addAll(newValues);
+ }
+ } else {
+ //also here do not add the collection if there is only one value!
+ representation.put(field, newValues.size() == 1?newValues.iterator().next():newValues);
+ }
+ }
+ protected void addValues(String field,Collection<Object> values){
+
+ }
+
+ @Override
+ public void addNaturalText(String field, String text, String... languages) {
+ if(languages == null || languages.length<1){ //if no language is parse add the default lanugage!
+ add(field,valueFactory.createText(text, null));
+ } else {
+ for(String lang : languages){
+ add(field,valueFactory.createText(text, lang));
+ }
+ }
+ }
+
+ @Override
+ public void addReference(String field, String reference) {
+ add(field, valueFactory.createReference(reference));
+ }
+ /**
+ * Getter for the values of the field as Collections. If the field is not
+ * present it returns an empty Collections!
+ * @param field the field
+ * @return A read only collection with the values of the field
+ */
+ @SuppressWarnings("unchecked")
+ private Collection<Object> getValuesAsCollection(String field){
+ Object value = representation.get(field);
+ if(value == null){
+ return Collections.emptySet();
+ } else if(value instanceof Collection<?>){
+ return (Collection<Object>)value;
+ } else {
+ return Collections.singleton(value);
+ }
+ }
+ @Override
+ public <T> Iterator<T> get(String field, Class<T> type) throws UnsupportedTypeException {
+ Collection<Object> values = getValuesAsCollection(field);
+ return new TypeSaveIterator<T>(values.iterator(), type);
+ }
+
+ @Override
+ public Iterator<Object> get(String field) {
+ return getValuesAsCollection(field).iterator();
+ }
+ @Override
+ public Iterator<Text> getText(String field) {
+ Collection<Object> values = getValuesAsCollection(field);
+ return values != null?new TextIterator(valueFactory,values.iterator()):null;
+ }
+
+ @Override
+ public Iterator<Text> get(String field, String... languages) {
+ final Collection<Object> values = getValuesAsCollection(field);
+ return new TextIterator(valueFactory,values.iterator(), languages);
+ }
+
+ @Override
+ public Iterator<String> getFieldNames() {
+ return unmodRepresentation.keySet().iterator();
+ }
+
+ @Override
+ public <T> T getFirst(String field, Class<T> type) throws UnsupportedTypeException {
+ Iterator<T> values = get(field,type);
+ return values.hasNext()?values.next():null;
+ }
+
+ @Override
+ public Object getFirst(String field) {
+ Iterator<Object> values = get(field);
+ return values.hasNext()?values.next():null;
+ }
+
+ @Override
+ public Text getFirst(String field, String... languages) {
+ Iterator<Text> values = get(field,languages);
+ return values.hasNext()?values.next():null;
+ }
+
+ @Override
+ public String getId() {
+ return id;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public void remove(String field, Object value) {
+ Object values = representation.get(field);
+ if(values == null) return;
+ if(value.equals(value)){
+ representation.remove(field);
+ } else if(values instanceof Collection<?>){
+ if(((Collection<Object>)values).remove(value) && //remove the Element
+ ((Collection<Object>)values).size()<2){ //if removed check for size
+ //it only one element remaining -> replace the collection with a Object
+ representation.put(field, ((Collection<Object>)values).iterator().next());
+ }
+
+ } //else ignore
+ }
+
+ @Override
+ public void removeAll(String field) {
+ representation.remove(field);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public void removeAllNaturalText(String field, String... languages) {
+ Object values = representation.get(field);
+ if(values == null) return;
+ if(values instanceof Collection<?>){
+ int removed = 0;
+ for(Iterator<Text> it = new TextIterator(valueFactory,
+ ((Collection<Object>)values).iterator(),
+ languages);it.hasNext();){
+ it.next();//go to the next Element
+ it.remove(); //and remove ist
+ removed++;
+ }
+ if(removed>0){ //if some elements where removed
+ //check if there is only a singe or no elements left for the field
+ int size = ((Collection<Object>)values).size();
+ if(size==1){
+ representation.put(field, ((Collection<Object>)values).iterator().next());
+ } else if(size<1){
+ representation.remove(field);
+ }
+ }
+ } else if(isNaturalLanguageValue(values, languages)){
+ representation.remove(field);
+ } //else there is a single value that does not fit -> nothing todo
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public void removeNaturalText(String field, String text, String... languages) {
+ Object values = representation.get(field);
+ if(values == null) return;
+ if(values instanceof Collection<?>){
+ int removed = 0;
+ for(Iterator<Text> it = new TextIterator(valueFactory,
+ ((Collection<Object>)values).iterator(),
+ languages);it.hasNext();){
+ Text label = it.next();//go to the next element
+ if(text.equals(label.getText())){
+ it.remove();//and remove it
+ removed++;
+ }
+ }
+ if(removed>0){ //if some elements where removed
+ //check if there is only a singe or no elements left for the field
+ int size = ((Collection<Object>)values).size();
+ if(size==1){
+ representation.put(field, ((Collection<Object>)values).iterator().next());
+ } else if(size<1){
+ representation.remove(field);
+ }
+ }
+ } else if(text.equals(getNaturalLanguageValue(values, languages))){
+ representation.remove(field);
+ } //else there is a single value that does not fit -> nothing todo
+
+ }
+
+ @Override
+ public void removeReference(String field, String reference) {
+ try {
+ remove(field,new URI(reference));
+ } catch (URISyntaxException e) {
+ throw new IllegalArgumentException("parsed reference needs to be an valid URI",e);
+ }
+ }
+
+ @Override
+ public void set(String field, Object value) {
+ representation.remove(field);
+ add(field,value);
+
+ }
+
+ @Override
+ public void setNaturalText(String field, String text, String... languages) {
+ removeAllNaturalText(field, languages);
+ if(text != null){
+ addNaturalText(field, text, languages);
+ }
+ }
+
+ @Override
+ public void setReference(String field, String reference) {
+ removeAll(field);
+ if(reference != null){
+ addReference(field, reference);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public Object clone() throws CloneNotSupportedException {
+ Map<String,Object> clone = new HashMap<String, Object>();
+ for(Entry<String,Object> e : representation.entrySet()){
+ if(e.getValue() instanceof HashSet<?>){
+ clone.put(e.getKey(), ((HashSet<?>)e.getValue()).clone());
+ } else if(e.getValue() instanceof Collection<?>){
+ HashSet<Object> valuesClone = new HashSet<Object>();
+ for(Iterator<Object> it = ((Collection<Object>)e.getValue()).iterator();it.hasNext();valuesClone.add(it.next()));
+ clone.put(e.getKey(), valuesClone);
+ } else {
+ clone.put(e.getKey(), e.getValue());
+ }
+ }
+ return new InMemoryRepresentation(id, clone);
+ }
+ @Override
+ public Reference getFirstReference(String field) {
+ Iterator<Reference> it = getReferences(field);
+ return it.hasNext()?it.next():null;
+ }
+ @Override
+ public Iterator<Reference> getReferences(String field) {
+ Collection<Object> values = getValuesAsCollection(field);
+ return new TypeSaveIterator<Reference>(values.iterator(), Reference.class);
+ }
+ private static String getNaturalLanguageValue(Object check,Set<String> langSet,boolean isNullLanguage){
+ if(check instanceof Text){
+ Text text = (Text)check;
+ if(langSet == null || langSet.contains(text.getLanguage())){
+ return text.getText();
+ } // else empty arrey -> filter
+ } else if(isNullLanguage && check instanceof String){
+ return (String)check;
+ } //type does not fit -> ignore
+ return null; //no label found
+ }
+ public static String getNaturalLanguageValue(Object check,String...languages){
+ Set<String> langSet;
+ boolean isNullLanguage;
+ if(languages != null && languages.length>1){
+ langSet = new HashSet<String>(Arrays.asList(languages));
+ isNullLanguage = langSet.contains(null);
+ } else {
+ langSet = null;
+ isNullLanguage = true;
+ }
+ return getNaturalLanguageValue(check,langSet,isNullLanguage);
+ }
+ /**
+ * @param check
+ * @param languages
+ * @return
+ */
+ public static boolean isNaturalLanguageValue(Object check,String...languages){
+ return getNaturalLanguageValue(check,languages) != null;
+ }
+ @Override
+ public String toString() {
+ return InMemoryRepresentation.class.getSimpleName()+getId();
+ }
+ @Override
+ public int hashCode() {
+ return getId().hashCode();
+ }
+ @Override
+ public boolean equals(Object obj) {
+ return obj != null && obj instanceof Representation && ((Representation)obj).getId().equals(getId());
+ }
}
Modified: incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/model/InMemoryValueFactory.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/model/InMemoryValueFactory.java?rev=1044829&r1=1044828&r2=1044829&view=diff
==============================================================================
--- incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/model/InMemoryValueFactory.java (original)
+++ incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/model/InMemoryValueFactory.java Sun Dec 12 15:02:34 2010
@@ -9,141 +9,141 @@ import eu.iksproject.rick.servicesapi.mo
public class InMemoryValueFactory implements ValueFactory {
- private static InMemoryValueFactory instance;
-
- public static InMemoryValueFactory getInstance(){
- if(instance == null){
- instance = new InMemoryValueFactory();
- }
- return instance;
- }
- protected InMemoryValueFactory(){
- super();
- }
-
- @Override
- public Reference createReference(Object value) {
- if(value == null){
- throw new IllegalArgumentException("The parsed value MUST NOT be NULL");
- }
- return new ReferenceImpl(value.toString());
- }
-
- @Override
- public Text createText(Object value) {
- if(value == null){
- throw new IllegalArgumentException("The parsed value MUST NOT be NULL");
- }
- return createText(value.toString(),null);
- }
-
- @Override
- public Text createText(String text, String language) {
- return new TextImpl(text,language);
- }
-
- protected static class ReferenceImpl implements Reference, Serializable,Cloneable{
-
- /**
- * serialVersionUID
- */
- private static final long serialVersionUID = 2571082550530948667L;
- private final String value;
-
- protected ReferenceImpl(String value) {
- super();
- if(value == null){
- throw new IllegalArgumentException("The value of the reference MUST NOT be NULL");
- }
- if(value.isEmpty()){
- throw new IllegalArgumentException("The value of the reference MUST NOT be empty");
- }
- this.value = value;
- }
-
- public final String getReference() {
- return value;
- }
- @Override
- public int hashCode() {
- return value.hashCode();
- }
-
- @Override
- public boolean equals(Object obj) {
- return obj != null && obj instanceof ReferenceImpl && ((ReferenceImpl)obj).value.equals(value);
- }
- @Override
- public String toString() {
- return value;
- }
- @Override
- public ReferenceImpl clone() throws CloneNotSupportedException {
- return new ReferenceImpl(value);
- }
- }
- protected static class TextImpl implements Text, Serializable,Cloneable {
-
- /**
- * serialVersionUID
- */
- private static final long serialVersionUID = -5646936810374934435L;
-
- private final String value;
- private final String language;
- protected TextImpl(String value) {
- this(value,null);
- }
- protected TextImpl(String value, String language) {
- super();
- if(value == null){
- throw new IllegalArgumentException("The value of the Text MUST NOT be NULL!");
- }
- this.value = value;
- this.language = language;
- }
- public final String getText() {
- return value;
- }
- public final String getLanguage() {
- return language;
- }
- @Override
- public int hashCode() {
- return value.hashCode()+(language!=null?language.hashCode():0);
- }
- @Override
- public String toString() {
- return value+(language!=null?('@'+language):"");
- }
- @Override
- public boolean equals(Object obj) {
- if(obj != null && obj instanceof TextImpl && ((TextImpl)obj).value.equals(value)){
- if(((TextImpl)obj).language == null){
- return language == null;
- } else {
- return ((TextImpl)obj).language.equals(language);
- }
- } else {
- return false;
- }
- }
- @Override
- protected Object clone() throws CloneNotSupportedException {
- return new TextImpl(value, language);
- }
- }
- @Override
- public Representation createRepresentation(String id) {
- if(id != null){
- return new InMemoryRepresentation(id);
- } else {
- throw new IllegalArgumentException("The parsed id MUST NOT be NULL");
- }
- }
-// @Override
-// public Object createValue(String dataTypeUri, Object value) throws UnsupportedTypeException, UnsupportedDataTypeException {
-//
-// return null;
-// }
+ private static InMemoryValueFactory instance;
+
+ public static InMemoryValueFactory getInstance(){
+ if(instance == null){
+ instance = new InMemoryValueFactory();
+ }
+ return instance;
+ }
+ protected InMemoryValueFactory(){
+ super();
+ }
+
+ @Override
+ public Reference createReference(Object value) {
+ if(value == null){
+ throw new IllegalArgumentException("The parsed value MUST NOT be NULL");
+ }
+ return new ReferenceImpl(value.toString());
+ }
+
+ @Override
+ public Text createText(Object value) {
+ if(value == null){
+ throw new IllegalArgumentException("The parsed value MUST NOT be NULL");
+ }
+ return createText(value.toString(),null);
+ }
+
+ @Override
+ public Text createText(String text, String language) {
+ return new TextImpl(text,language);
+ }
+
+ protected static class ReferenceImpl implements Reference, Serializable,Cloneable{
+
+ /**
+ * serialVersionUID
+ */
+ private static final long serialVersionUID = 2571082550530948667L;
+ private final String value;
+
+ protected ReferenceImpl(String value) {
+ super();
+ if(value == null){
+ throw new IllegalArgumentException("The value of the reference MUST NOT be NULL");
+ }
+ if(value.isEmpty()){
+ throw new IllegalArgumentException("The value of the reference MUST NOT be empty");
+ }
+ this.value = value;
+ }
+
+ public final String getReference() {
+ return value;
+ }
+ @Override
+ public int hashCode() {
+ return value.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return obj != null && obj instanceof ReferenceImpl && ((ReferenceImpl)obj).value.equals(value);
+ }
+ @Override
+ public String toString() {
+ return value;
+ }
+ @Override
+ public ReferenceImpl clone() throws CloneNotSupportedException {
+ return new ReferenceImpl(value);
+ }
+ }
+ protected static class TextImpl implements Text, Serializable,Cloneable {
+
+ /**
+ * serialVersionUID
+ */
+ private static final long serialVersionUID = -5646936810374934435L;
+
+ private final String value;
+ private final String language;
+ protected TextImpl(String value) {
+ this(value,null);
+ }
+ protected TextImpl(String value, String language) {
+ super();
+ if(value == null){
+ throw new IllegalArgumentException("The value of the Text MUST NOT be NULL!");
+ }
+ this.value = value;
+ this.language = language;
+ }
+ public final String getText() {
+ return value;
+ }
+ public final String getLanguage() {
+ return language;
+ }
+ @Override
+ public int hashCode() {
+ return value.hashCode()+(language!=null?language.hashCode():0);
+ }
+ @Override
+ public String toString() {
+ return value+(language!=null?('@'+language):"");
+ }
+ @Override
+ public boolean equals(Object obj) {
+ if(obj != null && obj instanceof TextImpl && ((TextImpl)obj).value.equals(value)){
+ if(((TextImpl)obj).language == null){
+ return language == null;
+ } else {
+ return ((TextImpl)obj).language.equals(language);
+ }
+ } else {
+ return false;
+ }
+ }
+ @Override
+ protected Object clone() throws CloneNotSupportedException {
+ return new TextImpl(value, language);
+ }
+ }
+ @Override
+ public Representation createRepresentation(String id) {
+ if(id != null){
+ return new InMemoryRepresentation(id);
+ } else {
+ throw new IllegalArgumentException("The parsed id MUST NOT be NULL");
+ }
+ }
+// @Override
+// public Object createValue(String dataTypeUri, Object value) throws UnsupportedTypeException, UnsupportedDataTypeException {
+//
+// return null;
+// }
}
Modified: incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/DefaultQueryFactory.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/DefaultQueryFactory.java?rev=1044829&r1=1044828&r2=1044829&view=diff
==============================================================================
--- incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/DefaultQueryFactory.java (original)
+++ incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/DefaultQueryFactory.java Sun Dec 12 15:02:34 2010
@@ -7,18 +7,24 @@ import eu.iksproject.rick.servicesapi.qu
* Simple {@link FieldQueryFactory} implementation that uses the singleton
* pattern and returns for each call to {@link #createFieldQuery()} a new
* instance of {@link FieldQueryImpl}.
- * @author Rupert Westenthaler
*
+ * @author Rupert Westenthaler
*/
public class DefaultQueryFactory implements FieldQueryFactory {
- private static final DefaultQueryFactory instance = new DefaultQueryFactory();
- public static FieldQueryFactory getInstance(){
- return instance;
- }
- protected DefaultQueryFactory() { super(); }
- @Override
- public FieldQuery createFieldQuery() {
- return new FieldQueryImpl();
- }
+
+ private static final DefaultQueryFactory instance = new DefaultQueryFactory();
+
+ public static FieldQueryFactory getInstance() {
+ return instance;
+ }
+
+ protected DefaultQueryFactory() {
+ super();
+ }
+
+ @Override
+ public FieldQuery createFieldQuery() {
+ return new FieldQueryImpl();
+ }
}
Modified: incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/FieldQueryImpl.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/FieldQueryImpl.java?rev=1044829&r1=1044828&r2=1044829&view=diff
==============================================================================
--- incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/FieldQueryImpl.java (original)
+++ incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/FieldQueryImpl.java Sun Dec 12 15:02:34 2010
@@ -24,165 +24,153 @@ import eu.iksproject.rick.servicesapi.qu
*
*/
public class FieldQueryImpl implements Cloneable, FieldQuery{
-
- @SuppressWarnings("unused")
- private static final Logger log = LoggerFactory.getLogger(FieldQueryImpl.class);
-
- protected final Map<String,Constraint> queryConstraint = new HashMap<String, Constraint>();
- private final Map<String,Constraint> unmodQueryElements = Collections.unmodifiableMap(queryConstraint);
-
- protected final Set<String> selected = new HashSet<String>();
- private final Set<String> unmodSelected = Collections.unmodifiableSet(selected);
-
- private Integer limit;
-
- private int offset;
-
- public FieldQueryImpl(){
- super();
- }
- /* (non-Javadoc)
- * @see eu.iksproject.rick.core.query.FieldQuery#addSelectedFields(java.lang.String)
- */
- public void addSelectedField(String field){
- if(field != null){
- selected.add(field);
- }
- }
- /* (non-Javadoc)
- * @see eu.iksproject.rick.core.query.FieldQuery#addSelectedFields(java.util.Collection)
- */
- public void addSelectedFields(Collection<String> fields){
- if(fields != null){
- selected.addAll(fields);
- }
- }
- /* (non-Javadoc)
- * @see eu.iksproject.rick.core.query.FieldQuery#removeSelectedFields(java.lang.String)
- */
- public void removeSelectedField(String field){
- if(field != null){
- selected.remove(field);
- }
- }
- /* (non-Javadoc)
- * @see eu.iksproject.rick.core.query.FieldQuery#removeSelectedFields(java.util.Collection)
- */
- public void removeSelectedFields(Collection<String> fields){
- if(fields != null){
- selected.removeAll(fields);
- }
- }
- /* (non-Javadoc)
- * @see eu.iksproject.rick.core.query.FieldQuery#getSelectedFields()
- */
- public final Set<String> getSelectedFields(){
- return unmodSelected;
- }
- /* (non-Javadoc)
- * @see eu.iksproject.rick.core.query.FieldQuery#setConstraint(java.lang.String, eu.iksproject.rick.core.query.Constraint)
- */
- public void setConstraint(String field,Constraint constraint){
- if(field != null && !field.isEmpty()){
- if(constraint == null){
- queryConstraint.remove(field);
- } else {
- queryConstraint.put(field, constraint);
- }
- } else {
- throw new IllegalArgumentException("Parameter Field MUST NOT be NULL nor empty!");
- }
- }
- /**
- * Calls {@link #setConstraint(String, Constraint)} with <code>null</code>
- * as {@link Constraint}. So overwrite the setConstraint Method if needed.
- * @see eu.iksproject.rick.core.query.FieldConstraint#removeConstraint(java.lang.String)
- */
- public final void removeConstraint(String field){
- setConstraint(field,null);
- }
- /* (non-Javadoc)
- * @see eu.iksproject.rick.core.query.FieldQuery#isConstraint(java.lang.String)
- */
- public final boolean isConstraint(String field){
- return queryConstraint.containsKey(field);
- }
- /* (non-Javadoc)
- * @see eu.iksproject.rick.core.query.FieldQuery#getConstraint(java.lang.String)
- */
- public final Constraint getConstraint(String field){
- return queryConstraint.get(field);
- }
- /* (non-Javadoc)
- * @see eu.iksproject.rick.core.query.FieldQuery#getConstraints()
- */
- @Override
- public Set<Entry<String,Constraint>> getConstraints(){
- return unmodQueryElements.entrySet();
- }
-
- @Override
- public final Iterator<Entry<String, Constraint>> iterator() {
- return unmodQueryElements.entrySet().iterator();
- }
-
- @Override
- public String toString() {
- return "Query constraints:"+queryConstraint+" selectedFields:"+selected;
- }
- @Override
- public FieldQuery clone() {
- return copyTo(new FieldQueryImpl());
- }
- /**
- * Uses the public API to clone the state of this instance to the instance
- * provided as parameter.
- * @param <C> An implementation of the FieldQuery interface
- * @param copyTo An instance to copy the state of this on.
- * @return The parsed instance
- */
- public <C extends FieldQuery> C copyTo(C copyTo){
- copyTo.removeAllConstraints();
- copyTo.removeAllSelectedFields();
- for(Entry<String,Constraint> entry : queryConstraint.entrySet()){
- //we need not to copy keys or values, because everything is immutable
- copyTo.setConstraint(entry.getKey(), entry.getValue());
- }
- copyTo.addSelectedFields(selected);
- return copyTo;
- }
- @Override
- public void removeAllConstraints() {
- selected.clear();
- }
- @Override
- public void removeAllSelectedFields() {
- queryConstraint.clear();
- }
- @Override
- public final String getQueryType() {
- return FieldQuery.TYPE;
- }
- @Override
- public final Integer getLimit() {
- return limit;
- }
- @Override
- public final int getOffset() {
- return offset;
- }
- @Override
- public final void setLimit(Integer limit) {
- if(limit != null && limit.intValue()<1){
- limit = null;
- }
- this.limit = limit;
- }
- @Override
- public final void setOffset(int offset) {
- if(offset < 0){
- offset = 0;
- }
- this.offset = offset;
- }
+
+ @SuppressWarnings("unused")
+ private static final Logger log = LoggerFactory.getLogger(FieldQueryImpl.class);
+
+ protected final Map<String,Constraint> queryConstraint = new HashMap<String, Constraint>();
+ private final Map<String,Constraint> unmodQueryElements = Collections.unmodifiableMap(queryConstraint);
+
+ protected final Set<String> selected = new HashSet<String>();
+ private final Set<String> unmodSelected = Collections.unmodifiableSet(selected);
+
+ private Integer limit;
+
+ private int offset;
+
+ public FieldQueryImpl(){
+ super();
+ }
+
+ public void addSelectedField(String field){
+ if(field != null){
+ selected.add(field);
+ }
+ }
+
+ public void addSelectedFields(Collection<String> fields){
+ if(fields != null){
+ selected.addAll(fields);
+ }
+ }
+
+ public void removeSelectedField(String field){
+ if(field != null){
+ selected.remove(field);
+ }
+ }
+
+ public void removeSelectedFields(Collection<String> fields){
+ if(fields != null){
+ selected.removeAll(fields);
+ }
+ }
+
+ public final Set<String> getSelectedFields(){
+ return unmodSelected;
+ }
+
+ public void setConstraint(String field,Constraint constraint){
+ if(field != null && !field.isEmpty()){
+ if(constraint == null){
+ queryConstraint.remove(field);
+ } else {
+ queryConstraint.put(field, constraint);
+ }
+ } else {
+ throw new IllegalArgumentException("Parameter Field MUST NOT be NULL nor empty!");
+ }
+ }
+ /**
+ * Calls {@link #setConstraint(String, Constraint)} with <code>null</code>
+ * as {@link Constraint}. So overwrite the setConstraint Method if needed.
+ * @see eu.iksproject.rick.core.query.FieldConstraint#removeConstraint(java.lang.String)
+ */
+ public final void removeConstraint(String field){
+ setConstraint(field,null);
+ }
+ /* (non-Javadoc)
+ * @see eu.iksproject.rick.core.query.FieldQuery#isConstraint(java.lang.String)
+ */
+ public final boolean isConstraint(String field){
+ return queryConstraint.containsKey(field);
+ }
+ /* (non-Javadoc)
+ * @see eu.iksproject.rick.core.query.FieldQuery#getConstraint(java.lang.String)
+ */
+ public final Constraint getConstraint(String field){
+ return queryConstraint.get(field);
+ }
+ /* (non-Javadoc)
+ * @see eu.iksproject.rick.core.query.FieldQuery#getConstraints()
+ */
+ @Override
+ public Set<Entry<String,Constraint>> getConstraints(){
+ return unmodQueryElements.entrySet();
+ }
+
+ @Override
+ public final Iterator<Entry<String, Constraint>> iterator() {
+ return unmodQueryElements.entrySet().iterator();
+ }
+
+ @Override
+ public String toString() {
+ return "Query constraints:"+queryConstraint+" selectedFields:"+selected;
+ }
+ @Override
+ public FieldQuery clone() {
+ return copyTo(new FieldQueryImpl());
+ }
+ /**
+ * Uses the public API to clone the state of this instance to the instance
+ * provided as parameter.
+ * @param <C> An implementation of the FieldQuery interface
+ * @param copyTo An instance to copy the state of this on.
+ * @return The parsed instance
+ */
+ public <C extends FieldQuery> C copyTo(C copyTo){
+ copyTo.removeAllConstraints();
+ copyTo.removeAllSelectedFields();
+ for(Entry<String,Constraint> entry : queryConstraint.entrySet()){
+ //we need not to copy keys or values, because everything is immutable
+ copyTo.setConstraint(entry.getKey(), entry.getValue());
+ }
+ copyTo.addSelectedFields(selected);
+ return copyTo;
+ }
+ @Override
+ public void removeAllConstraints() {
+ selected.clear();
+ }
+ @Override
+ public void removeAllSelectedFields() {
+ queryConstraint.clear();
+ }
+ @Override
+ public final String getQueryType() {
+ return FieldQuery.TYPE;
+ }
+ @Override
+ public final Integer getLimit() {
+ return limit;
+ }
+ @Override
+ public final int getOffset() {
+ return offset;
+ }
+ @Override
+ public final void setLimit(Integer limit) {
+ if(limit != null && limit.intValue()<1){
+ limit = null;
+ }
+ this.limit = limit;
+ }
+ @Override
+ public final void setOffset(int offset) {
+ if(offset < 0){
+ offset = 0;
+ }
+ this.offset = offset;
+ }
}
Modified: incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/QueryResultListImpl.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/QueryResultListImpl.java?rev=1044829&r1=1044828&r2=1044829&view=diff
==============================================================================
--- incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/QueryResultListImpl.java (original)
+++ incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/QueryResultListImpl.java Sun Dec 12 15:02:34 2010
@@ -11,92 +11,93 @@ import eu.iksproject.rick.servicesapi.qu
import eu.iksproject.rick.servicesapi.query.QueryResultList;
public class QueryResultListImpl<T> implements Iterable<T>, QueryResultList<T>{
-
-
- private final Collection<T> results;
- private final FieldQuery query;
- private Class<T> type;
- /**
- * Constructs an QueryResultList by iterating over all elements in the parsed
- * {@link Iterator} and storing all elements that are NOT <code>null</code>.
- * @param query The query uses to select the results
- * @param resultIterator The Iterator containing the results of the Query
- * @throws IllegalArgumentException if the parsed {@link FieldQuery} is <code>null</code>
- */
- public QueryResultListImpl(FieldQuery query,Iterator<T> resultIterator,Class<T> type) throws IllegalArgumentException {
-// if(query == null){
-// throw new IllegalArgumentException("Query MUST NOT be NULL");
-// }
- this.query = query;
- if(type == null){
- throw new IllegalArgumentException("The type of the results MUST NOT be NULL");
- }
- this.type = type;
- if(resultIterator == null || !resultIterator.hasNext()){
- this.results = Collections.emptyList();
- } else {
- List<T> results = new ArrayList<T>();
- while(resultIterator.hasNext()){
- results.add(resultIterator.next());
- }
- this.results = Collections.unmodifiableList(results);
- }
- }
- @Override
- public Class<T> getType(){
- return type;
- }
- /**
- * Constructs an QueryResultList with the parsed Query and Results
- * @param query The query uses to select the results
- * @param results The results of the query
- * @throws IllegalArgumentException if the parsed {@link FieldQuery} is <code>null</code>
- */
- public QueryResultListImpl(FieldQuery query,Collection<T> results,Class<T> type) throws IllegalArgumentException {
-// if(query == null){
-// throw new IllegalArgumentException("Query MUST NOT be NULL");
-// }
- this.query = query;
- if(type == null){
- throw new IllegalArgumentException("The type of the results MUST NOT be NULL");
- }
- this.type = type;
- if(results == null){
- this.results = Collections.emptyList();
- } else {
- this.results = Collections.unmodifiableCollection(results);
- }
- }
- /* (non-Javadoc)
- * @see eu.iksproject.rick.core.query.ResultList#getQuery()
- */
- public FieldQuery getQuery(){
- return query;
- }
- /* (non-Javadoc)
- * @see eu.iksproject.rick.core.query.ResultList#getSelectedFields()
- */
- public Set<String> getSelectedFields(){
- return query.getSelectedFields();
- }
- /* (non-Javadoc)
- * @see eu.iksproject.rick.core.query.ResultList#iterator()
- */
- @Override
- public Iterator<T> iterator() {
- return results.iterator();
- }
- /* (non-Javadoc)
- * @see eu.iksproject.rick.core.query.ResultList#isEmpty()
- */
- public boolean isEmpty() {
- return results.isEmpty();
- }
- /* (non-Javadoc)
- * @see eu.iksproject.rick.core.query.ResultList#size()
- */
- public int size() {
- return results.size(); //not supported :(
- }
+
+ private final Collection<T> results;
+ private final FieldQuery query;
+ private Class<T> type;
+
+ /**
+ * Constructs an QueryResultList by iterating over all elements in the parsed
+ * {@link Iterator} and storing all elements that are NOT <code>null</code>.
+ * @param query The query uses to select the results
+ * @param resultIterator The Iterator containing the results of the Query
+ * @throws IllegalArgumentException if the parsed {@link FieldQuery} is <code>null</code>
+ */
+ public QueryResultListImpl(FieldQuery query,Iterator<T> resultIterator,Class<T> type) throws IllegalArgumentException {
+// if(query == null){
+// throw new IllegalArgumentException("Query MUST NOT be NULL");
+// }
+ this.query = query;
+ if(type == null){
+ throw new IllegalArgumentException("The type of the results MUST NOT be NULL");
+ }
+ this.type = type;
+ if(resultIterator == null || !resultIterator.hasNext()){
+ this.results = Collections.emptyList();
+ } else {
+ List<T> results = new ArrayList<T>();
+ while(resultIterator.hasNext()){
+ results.add(resultIterator.next());
+ }
+ this.results = Collections.unmodifiableList(results);
+ }
+ }
+ @Override
+ public Class<T> getType(){
+ return type;
+ }
+ /**
+ * Constructs an QueryResultList with the parsed Query and Results
+ * @param query The query uses to select the results
+ * @param results The results of the query
+ * @throws IllegalArgumentException if the parsed {@link FieldQuery} is <code>null</code>
+ */
+ public QueryResultListImpl(FieldQuery query,Collection<T> results,Class<T> type)
+ throws IllegalArgumentException {
+// if(query == null){
+// throw new IllegalArgumentException("Query MUST NOT be NULL");
+// }
+ this.query = query;
+ if(type == null){
+ throw new IllegalArgumentException("The type of the results MUST NOT be NULL");
+ }
+ this.type = type;
+ if(results == null){
+ this.results = Collections.emptyList();
+ } else {
+ this.results = Collections.unmodifiableCollection(results);
+ }
+ }
+ /* (non-Javadoc)
+ * @see eu.iksproject.rick.core.query.ResultList#getQuery()
+ */
+ public FieldQuery getQuery(){
+ return query;
+ }
+ /* (non-Javadoc)
+ * @see eu.iksproject.rick.core.query.ResultList#getSelectedFields()
+ */
+ public Set<String> getSelectedFields(){
+ return query.getSelectedFields();
+ }
+ /* (non-Javadoc)
+ * @see eu.iksproject.rick.core.query.ResultList#iterator()
+ */
+ @Override
+ public Iterator<T> iterator() {
+ return results.iterator();
+ }
+ /* (non-Javadoc)
+ * @see eu.iksproject.rick.core.query.ResultList#isEmpty()
+ */
+ public boolean isEmpty() {
+ return results.isEmpty();
+ }
+ /* (non-Javadoc)
+ * @see eu.iksproject.rick.core.query.ResultList#size()
+ */
+ public int size() {
+ return results.size(); //not supported :(
+ }
}
Modified: incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/QueryUtils.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/QueryUtils.java?rev=1044829&r1=1044828&r2=1044829&view=diff
==============================================================================
--- incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/QueryUtils.java (original)
+++ incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/query/QueryUtils.java Sun Dec 12 15:02:34 2010
@@ -8,28 +8,28 @@ import eu.iksproject.rick.servicesapi.qu
*
*/
public class QueryUtils {
-
- /**
- * Getter for the Limit calculated bye on the limit defined by the query
- * and the configuration of the default results (for queries that do not
- * define a limit) and the maximum number of Results.<p>
- * Configurations for defaultResults and maxResults <= 0 are ignored. Return
- * values < = 0 should be interpreted as no constraints.
- * @param query the query
- * @param defaultResults the default number of results
- * @param maxResults the maximum number of queries
- * @return if > 0, than the value represents the number of results for the
- * query. Otherwise no constraint.
- */
- public static int getLimit(Query query,int defaultResults, int maxResults){
- int limit = query.getLimit() != null?query.getLimit():-1;
- if(defaultResults > 0){
- limit = Math.min(limit, defaultResults);
- }
- if(maxResults > 0){
- limit = Math.min(limit,maxResults);
- }
- return limit;
- }
+
+ /**
+ * Getter for the Limit calculated bye on the limit defined by the query
+ * and the configuration of the default results (for queries that do not
+ * define a limit) and the maximum number of Results.<p>
+ * Configurations for defaultResults and maxResults <= 0 are ignored. Return
+ * values < = 0 should be interpreted as no constraints.
+ * @param query the query
+ * @param defaultResults the default number of results
+ * @param maxResults the maximum number of queries
+ * @return if > 0, than the value represents the number of results for the
+ * query. Otherwise no constraint.
+ */
+ public static int getLimit(Query query,int defaultResults, int maxResults){
+ int limit = query.getLimit() != null?query.getLimit():-1;
+ if(defaultResults > 0){
+ limit = Math.min(limit, defaultResults);
+ }
+ if(maxResults > 0){
+ limit = Math.min(limit,maxResults);
+ }
+ return limit;
+ }
}
Modified: incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/site/AbstractEntityDereferencer.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/site/AbstractEntityDereferencer.java?rev=1044829&r1=1044828&r2=1044829&view=diff
==============================================================================
--- incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/site/AbstractEntityDereferencer.java (original)
+++ incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/site/AbstractEntityDereferencer.java Sun Dec 12 15:02:34 2010
@@ -16,104 +16,111 @@ import org.slf4j.Logger;
import eu.iksproject.rick.servicesapi.site.ConfiguredSite;
import eu.iksproject.rick.servicesapi.site.EntityDereferencer;
-@Property(name=EntityDereferencer.ACCESS_URI,label="%dereference.baseUri.name",description="%dereference.baseUri.description")
+
+@Property(name = EntityDereferencer.ACCESS_URI, label = "%dereference.baseUri.name", description = "%dereference.baseUri.description")
public abstract class AbstractEntityDereferencer implements EntityDereferencer {
- protected final Logger log;
-
- protected AbstractEntityDereferencer(Logger log){
- this.log = log;
- log.info("create instance of "+this.getClass().getName());
- }
-
- private String baseUri;
-
- private Dictionary<String,?> config;
- private List<String> prefixes;
- private ComponentContext context;
-
- @Override
- public final String getAccessUri() {
- return baseUri;
- }
-
- @SuppressWarnings("unchecked")
- @Activate
- protected void activate(ComponentContext context) {
- log.info("in "+AbstractEntityDereferencer.class.getSimpleName()+" activate with context "+context);
- // TODO handle updates to the configuration
- if(context != null && context.getProperties() != null){
- this.context = context;
- Dictionary<String,?> properties = context.getProperties();
- Object baseUri = properties.get(EntityDereferencer.ACCESS_URI);
- if(baseUri != null){
- this.baseUri = baseUri.toString();
- //now set the new config
- this.config = properties;
- } else {
- throw new IllegalArgumentException("The property "+EntityDereferencer.ACCESS_URI+" must be defined");
- }
- //TODO: I am sure, there is some Utility, that supports getting multiple
- // values from a OSGI Dictionary
- Object prefixObject = properties.get(ConfiguredSite.ENTITY_PREFIX);
- ArrayList<String> prefixes = new ArrayList<String>();
- if(prefixObject == null){
- prefixes = null;
- } else if(prefixObject.getClass().isArray()){
- prefixes.addAll(Arrays.asList((String[])prefixObject));
- } else if(prefixObject instanceof Collection<?>){
- prefixes.addAll((Collection<String>)prefixObject);
- } else { //assuming a single value
- prefixes.add(prefixObject.toString());
- }
- Collections.sort(prefixes); //sort the prefixes List
- this.prefixes = Collections.unmodifiableList(prefixes); //use an unmodifiable wrapper for the member variable
- } else {
- throw new IllegalArgumentException("The property "+EntityDereferencer.ACCESS_URI+" must be defined");
- }
-
- }
- @Deactivate
- protected void deactivate(ComponentContext context) {
- log.info("in "+AbstractEntityDereferencer.class.getSimpleName()+" deactivate with context "+context);
+ protected final Logger log;
+
+ protected AbstractEntityDereferencer(Logger log) {
+ this.log = log;
+ log.info("create instance of " + this.getClass().getName());
+ }
+
+ private String baseUri;
+
+ private Dictionary<String, ?> config;
+ private List<String> prefixes;
+ private ComponentContext context;
+
+ @Override
+ public final String getAccessUri() {
+ return baseUri;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Activate
+ protected void activate(ComponentContext context) {
+ log.info("in " + AbstractEntityDereferencer.class.getSimpleName() + " activate with context " + context);
+ // TODO handle updates to the configuration
+ if (context != null && context.getProperties() != null) {
+ this.context = context;
+ Dictionary<String, ?> properties = context.getProperties();
+ Object baseUri = properties.get(EntityDereferencer.ACCESS_URI);
+ if (baseUri != null) {
+ this.baseUri = baseUri.toString();
+ //now set the new config
+ this.config = properties;
+ } else {
+ throw new IllegalArgumentException("The property " + EntityDereferencer.ACCESS_URI + " must be defined");
+ }
+ //TODO: I am sure, there is some Utility, that supports getting multiple
+ // values from a OSGI Dictionary
+ Object prefixObject = properties.get(ConfiguredSite.ENTITY_PREFIX);
+ ArrayList<String> prefixes = new ArrayList<String>();
+ if (prefixObject == null) {
+ prefixes = null;
+ } else if (prefixObject.getClass().isArray()) {
+ prefixes.addAll(Arrays.asList((String[]) prefixObject));
+ } else if (prefixObject instanceof Collection<?>) {
+ prefixes.addAll((Collection<String>) prefixObject);
+ } else { //assuming a single value
+ prefixes.add(prefixObject.toString());
+ }
+ Collections.sort(prefixes); //sort the prefixes List
+ this.prefixes = Collections.unmodifiableList(prefixes); //use an unmodifiable wrapper for the member variable
+ } else {
+ throw new IllegalArgumentException("The property " + EntityDereferencer.ACCESS_URI + " must be defined");
+ }
+
+ }
+
+ @Deactivate
+ protected void deactivate(ComponentContext context) {
+ log.info("in " + AbstractEntityDereferencer.class.getSimpleName() + " deactivate with context " + context);
this.config = null;
this.prefixes = null;
this.baseUri = null;
}
- /**
- * Default Implementation based on the configured EntityPrefixes that
- * executes in O(log n) by using a binary search
- * {@link Collections#binarySearch(List, Object)} in the list of prefixes
- * and than checking if the parsed uri starts with the prefix of the
- * "insertion point-1" as returned by the binary search
- */
- @Override
- public boolean canDereference(String uri) {
- //binary search for the uri returns "insertion point-1"
- int pos = Collections.binarySearch(prefixes, uri);
- //This site can dereference the URI if
- // - the pos >=0 (parsed uri > than the first element in the list AND
- // - the returned index is an prefix of the parsed uri
- return pos >= 0 && uri.startsWith(prefixes.get(pos));
- }
- /**
- * The prefixes as configured for this dereferencer as unmodifiable and
- * sorted list guaranteed to implementing {@link RandomAccess}.
- * @return the prefixes in an unmodifiable and sorted list that implements
- * {@link RandomAccess}.
- */
- protected final List<String> getPrefixes(){
- return prefixes;
- }
- /**
- * The OSGI configuration as provided by the activate method
- * @return
- */
- protected final Dictionary<String,?> getSiteConfiguration() {
- return config;
- }
-
- protected final ComponentContext getComponentContext(){
- return context;
- }
+
+ /**
+ * Default Implementation based on the configured EntityPrefixes that
+ * executes in O(log n) by using a binary search
+ * {@link Collections#binarySearch(List, Object)} in the list of prefixes
+ * and than checking if the parsed uri starts with the prefix of the
+ * "insertion point-1" as returned by the binary search
+ */
+ @Override
+ public boolean canDereference(String uri) {
+ //binary search for the uri returns "insertion point-1"
+ int pos = Collections.binarySearch(prefixes, uri);
+ //This site can dereference the URI if
+ // - the pos >=0 (parsed uri > than the first element in the list AND
+ // - the returned index is an prefix of the parsed uri
+ return pos >= 0 && uri.startsWith(prefixes.get(pos));
+ }
+
+ /**
+ * The prefixes as configured for this dereferencer as unmodifiable and
+ * sorted list guaranteed to implementing {@link RandomAccess}.
+ *
+ * @return the prefixes in an unmodifiable and sorted list that implements
+ * {@link RandomAccess}.
+ */
+ protected final List<String> getPrefixes() {
+ return prefixes;
+ }
+
+ /**
+ * The OSGI configuration as provided by the activate method
+ *
+ * @return
+ */
+ protected final Dictionary<String, ?> getSiteConfiguration() {
+ return config;
+ }
+
+ protected final ComponentContext getComponentContext() {
+ return context;
+ }
}
Modified: incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/site/AbstractEntitySearcher.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/site/AbstractEntitySearcher.java?rev=1044829&r1=1044828&r2=1044829&view=diff
==============================================================================
--- incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/site/AbstractEntitySearcher.java (original)
+++ incubator/stanbol/trunk/rick/generic/core/src/main/java/eu/iksproject/rick/core/site/AbstractEntitySearcher.java Sun Dec 12 15:02:34 2010
@@ -14,62 +14,62 @@ import eu.iksproject.rick.servicesapi.si
@Property(name=ConfiguredSite.QUERY_URI)
public abstract class AbstractEntitySearcher implements EntitySearcher {
- protected final Logger log;
-
- protected AbstractEntitySearcher(Logger log){
- this.log = log;
- log.info("create instance of "+this.getClass().getName());
- }
-
- private String queryUri;
-
- private Dictionary<String,?> config;
- private ComponentContext context;
-
- protected final String getQueryUri() {
- return queryUri;
- }
-
- @SuppressWarnings("unchecked")
- @Activate
- protected void activate(ComponentContext context) {
- log.debug("in "+AbstractEntitySearcher.class.getSimpleName()+" activate with context "+context);
- // TODO handle updates to the configuration
- if(context != null && context.getProperties() != null){
- this.context = context;
- Dictionary<String,?> properties = context.getProperties();
- Object queryUri = properties.get(EntitySearcher.QUERY_URI);
- Object accessUri = properties.get(ConfiguredSite.ACCESS_URI); //use as an fallback
- if(queryUri != null){
- this.queryUri = queryUri.toString();
- //now set the new config
- } else if(accessUri != null){
- log.info("Using AccessUri as fallback for missing QueryUri Proerty (accessUri="+accessUri);
- this.queryUri = accessUri.toString();
- } else {
- throw new IllegalArgumentException("The property "+EntitySearcher.QUERY_URI+" must be defined");
- }
- this.config = properties;
- } else {
- throw new IllegalArgumentException("The property "+EntitySearcher.QUERY_URI+" must be defined");
- }
-
- }
- @Deactivate
- protected void deactivate(ComponentContext context) {
- log.debug("in "+AbstractEntitySearcher.class.getSimpleName()+" deactivate with context "+context);
+ protected final Logger log;
+
+ protected AbstractEntitySearcher(Logger log){
+ this.log = log;
+ log.info("create instance of "+this.getClass().getName());
+ }
+
+ private String queryUri;
+
+ private Dictionary<String,?> config;
+ private ComponentContext context;
+
+ protected final String getQueryUri() {
+ return queryUri;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Activate
+ protected void activate(ComponentContext context) {
+ log.debug("in "+AbstractEntitySearcher.class.getSimpleName()+" activate with context "+context);
+ // TODO handle updates to the configuration
+ if(context != null && context.getProperties() != null){
+ this.context = context;
+ Dictionary<String,?> properties = context.getProperties();
+ Object queryUri = properties.get(EntitySearcher.QUERY_URI);
+ Object accessUri = properties.get(ConfiguredSite.ACCESS_URI); //use as an fallback
+ if(queryUri != null){
+ this.queryUri = queryUri.toString();
+ //now set the new config
+ } else if(accessUri != null){
+ log.info("Using AccessUri as fallback for missing QueryUri Proerty (accessUri="+accessUri);
+ this.queryUri = accessUri.toString();
+ } else {
+ throw new IllegalArgumentException("The property "+EntitySearcher.QUERY_URI+" must be defined");
+ }
+ this.config = properties;
+ } else {
+ throw new IllegalArgumentException("The property "+EntitySearcher.QUERY_URI+" must be defined");
+ }
+
+ }
+ @Deactivate
+ protected void deactivate(ComponentContext context) {
+ log.debug("in "+AbstractEntitySearcher.class.getSimpleName()+" deactivate with context "+context);
this.config = null;
this.queryUri = null;
}
- /**
- * The OSGI configuration as provided by the activate method
- * @return
- */
- protected final Dictionary<String,?> getSiteConfiguration() {
- return config;
- }
-
- protected final ComponentContext getComponentContext(){
- return context;
- }
+ /**
+ * The OSGI configuration as provided by the activate method
+ * @return
+ */
+ protected final Dictionary<String,?> getSiteConfiguration() {
+ return config;
+ }
+
+ protected final ComponentContext getComponentContext(){
+ return context;
+ }
}