jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [2/6] jena git commit: Add getInt and getLong.
Date Tue, 02 Jun 2015 10:41:05 GMT
Add getInt and getLong.

Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/faa8b59d
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/faa8b59d
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/faa8b59d

Branch: refs/heads/master
Commit: faa8b59dd897baa847099803f85de6dff17ff41f
Parents: 2a9b731
Author: Andy Seaborne <andy@apache.org>
Authored: Tue Jun 2 11:11:12 2015 +0100
Committer: Andy Seaborne <andy@apache.org>
Committed: Tue Jun 2 11:11:12 2015 +0100

----------------------------------------------------------------------
 .../org/apache/jena/sparql/util/Context.java    | 404 +++++++++++--------
 .../apache/jena/sparql/core/TestContext.java    | 102 +++--
 2 files changed, 299 insertions(+), 207 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/faa8b59d/jena-arq/src/main/java/org/apache/jena/sparql/util/Context.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/util/Context.java b/jena-arq/src/main/java/org/apache/jena/sparql/util/Context.java
index f8c8ed9..6593271 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/util/Context.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/util/Context.java
@@ -1,94 +1,105 @@
 /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
  */
 
-package org.apache.jena.sparql.util;
+package org.apache.jena.sparql.util ;
 
 import java.util.* ;
 
 import org.apache.jena.atlas.lib.Callback ;
+import org.apache.jena.atlas.lib.Lib ;
 import org.apache.jena.query.ARQ ;
 import org.apache.jena.sparql.ARQConstants ;
 import org.apache.jena.sparql.ARQException ;
 import org.apache.jena.sparql.core.DatasetGraph ;
 
-/** A class for setting and keeping named values.  Used to pass 
- *  implementation-specific parameters across general
- *  interfaces. */  
+/**
+ * A class for setting and keeping named values. Used to pass
+ * implementation-specific parameters across general interfaces.
+ */
+
+public class Context {
+    public static final Context      emptyContext = new Context(true) ;
+
+    protected Map<Symbol, Object>    context      = new HashMap<>() ;
+    protected List<Callback<Symbol>> callbacks    = new ArrayList<>() ;
+    protected boolean                readonly     = false ;
 
-public class Context
-{
-    public static final Context emptyContext = new Context(true) ;
-    
-    protected Map<Symbol, Object> context = new HashMap<>() ;
-    protected List<Callback<Symbol>> callbacks = new ArrayList<>() ;
-    protected boolean readonly = false ;
-    
     /** Create an empty context */
-    public Context()
-    { }
-    
+    public Context() {}
+
     /* Create an empty context, mark it's readonly state */
-    private Context(boolean readonly)
-    { 
+    private Context(boolean readonly) {
         this.readonly = readonly ;
     }
 
-    /** Create a context and initialize it with a copy of the named values of another one.
-     *  Shallow copy: the values themselves are not copied
-     */ 
-    public Context(Context cxt)
-    { putAll(cxt) ; }
-    
-    /** Return a copy of this context.  Modifications of the copy 
-     * do not affect the original context.
-     */ 
-    public Context copy() { return new Context(this) ; }
-    
+    /**
+     * Create a context and initialize it with a copy of the named values of
+     * another one. Shallow copy: the values themselves are not copied
+     */
+    public Context(Context cxt) {
+        putAll(cxt) ;
+    }
+
+    /**
+     * Return a copy of this context. Modifications of the copy do not affect
+     * the original context.
+     */
+    public Context copy() {
+        return new Context(this) ;
+    }
+
     // -- basic operations
-    
-    /** Get the object value of a property or null */ 
-    public Object get(Symbol property) { return context.get(property) ; }
-    
-    /** Get the object value of a property - return the default value if not present . */

-    public Object get(Symbol property, Object defaultValue)
-    { 
+
+    /** Get the object value of a property or null */
+    public Object get(Symbol property) {
+        return context.get(property) ;
+    }
+
+    /**
+     * Get the object value of a property - return the default value if not
+     * present .
+     */
+    public Object get(Symbol property, Object defaultValue) {
         Object x = context.get(property) ;
         if ( x == null )
             return defaultValue ;
         return x ;
     }
-    
+
     /** Store a named value - overwrites any previous set value */
-    public void put(Symbol property, Object value) { _put(property, value) ; doCallbacks(property)
; }
-    
+    public void put(Symbol property, Object value) {
+        _put(property, value) ;
+        doCallbacks(property) ;
+    }
+
     /** Store a named value - overwrites any previous set value */
-    public void set(Symbol property, Object value) { _put(property, value) ; doCallbacks(property)
; }
-    
-    private void _put(Symbol property, Object value)
-    {
+    public void set(Symbol property, Object value) {
+        _put(property, value) ;
+        doCallbacks(property) ;
+    }
+
+    private void _put(Symbol property, Object value) {
         if ( readonly )
             throw new ARQException("Context is readonly") ;
-        context.put(property, value) ; 
+        context.put(property, value) ;
     }
 
     /** Store a named value - overwrites any previous set value */
-    public void   set(Symbol property, boolean value)
-    { 
+    public void set(Symbol property, boolean value) {
         if ( value )
             setTrue(property) ;
         else
@@ -96,34 +107,45 @@ public class Context
     }
 
     /** Store a named value only if it is not currently set */
-    public void  setIfUndef(Symbol property, Object value)
-    { 
+    public void setIfUndef(Symbol property, Object value) {
         Object x = context.get(property) ;
         if ( x == null )
             put(property, value) ;
     }
 
     /** Remove any value associated with a property */
-    public void remove(Symbol property) { context.remove(property) ; doCallbacks(property)
; }
+    public void remove(Symbol property) {
+        context.remove(property) ;
+        doCallbacks(property) ;
+    }
 
     /** Remove any value associated with a property - alternative method name */
-    public void unset(Symbol property) { context.remove(property) ; doCallbacks(property)
; }
-    
+    public void unset(Symbol property) {
+        context.remove(property) ;
+        doCallbacks(property) ;
+    }
+
     // ---- Helpers
-    
+
     // -- Existence
-    
+
     /** Is a property set? */
-    public boolean isDefined(Symbol property) { return context.containsKey(property) ; }
+    public boolean isDefined(Symbol property) {
+        return context.containsKey(property) ;
+    }
 
     /** Is a property not set? */
-    public boolean isUndef(Symbol property) { return ! isDefined(property) ; }
-    
+    public boolean isUndef(Symbol property) {
+        return !isDefined(property) ;
+    }
+
     // -- as string
 
-    /** Get the value a string (uses .toString() if the value is not null) - supply a default
string value */
-    public String getAsString(Symbol property, String defaultValue)
-    {
+    /**
+     * Get the value a string (uses .toString() if the value is not null) -
+     * supply a default string value
+     */
+    public String getAsString(Symbol property, String defaultValue) {
         String x = getAsString(property) ;
         if ( x == null )
             return defaultValue ;
@@ -131,87 +153,119 @@ public class Context
     }
 
     /** Get the value a string (uses .toString() if the value is not null) */
-    public String getAsString(Symbol property)
-    { 
+    public String getAsString(Symbol property) {
         Object x = context.get(property) ;
         if ( x == null )
             return null ;
         return x.toString() ;
     }
+    
+    /** Get the value as a a long value. The context entry can be a string, Integer or Long.
*/
+    public int getInt(Symbol symbol, int defaultValue) {
+        if (  isUndef(symbol) )
+            return defaultValue ; 
+        Object obj = context.get(symbol) ;
+        if ( obj instanceof String ) {
+            return Integer.parseInt((String)obj) ;
+        } else if ( obj instanceof Integer ) {
+            return ((Integer)obj).intValue() ;
+        } else {
+            throw new ARQException("Value for "+symbol+" is not a recoginized class: "+Lib.className(obj))
;
+        }
+    }
 
-    public void putAll(Context other)
-    {
+    /** Get the value as a a long value. The context entry can be a string, Integer or Long.
*/
+    public long getLong(Symbol symbol, long defaultValue) {
+        if (  isUndef(symbol) )
+            return defaultValue ; 
+        Object obj = context.get(symbol) ;
+        if ( obj instanceof String ) {
+            return Long.parseLong((String)obj) ;
+        } else if ( obj instanceof Integer ) {
+            return ((Integer)obj).intValue() ;
+        } else if ( obj instanceof Long ) {
+            return ((Long)obj) ;
+        } else {
+            throw new ARQException("Value for "+symbol+" is not a recoginized class: "+Lib.className(obj))
;
+        }
+    }
+    
+    public void putAll(Context other) {
         if ( readonly )
             throw new ARQException("Context is readonly") ;
-        if ( other != null )
-        {
+        if ( other != null ) {
             for ( Map.Entry<Symbol, Object> e : other.context.entrySet() )
                 put(e.getKey(), e.getValue()) ;
         }
     }
-    
+
     // -- true/false
-    
+
     /** Set propety value to be true */
-    public void setTrue(Symbol property) { set(property, Boolean.TRUE) ; } 
-    
+    public void setTrue(Symbol property) {
+        set(property, Boolean.TRUE) ;
+    }
+
     /** Set propety value to be false */
-    public void setFalse(Symbol property) { set(property, Boolean.FALSE) ; } 
+    public void setFalse(Symbol property) {
+        set(property, Boolean.FALSE) ;
+    }
 
     /** Is the value 'true' (either set to the string "true" or Boolean.TRUE) */
-    public boolean isTrue(Symbol property)
-    { return isTrue(property, false) ; } 
-    
-    /** Is the value 'true' (either set to the string "true" or Boolean.TRUE) 
-     * or undefined? 
+    public boolean isTrue(Symbol property) {
+        return isTrue(property, false) ;
+    }
+
+    /**
+     * Is the value 'true' (either set to the string "true" or Boolean.TRUE) or
+     * undefined?
      */
-    public boolean isTrueOrUndef(Symbol property)
-    { return isTrue(property, true) ; }
-    
-    private boolean isTrue(Symbol property, boolean dft)
-    {
+    public boolean isTrueOrUndef(Symbol property) {
+        return isTrue(property, true) ;
+    }
+
+    private boolean isTrue(Symbol property, boolean dft) {
         Object x = get(property) ;
         if ( x == null )
             return dft ;
-        if ( x instanceof String )
-        {
+        if ( x instanceof String ) {
             String s = (String)x ;
-            if ( s.equalsIgnoreCase("true") ) 
+            if ( s.equalsIgnoreCase("true") )
                 return true ;
         }
         return x.equals(Boolean.TRUE) ;
     }
-    
+
     /** Is the value 'false' (either set to the string "false" or Boolean.FALSE) */
-    public boolean isFalse(Symbol property)
-    { return isFalse(property, false) ; } 
-    
-    /** Is the value 'false' (either set to the string "false" or Boolean.FALSE) 
-     * or undefined 
+    public boolean isFalse(Symbol property) {
+        return isFalse(property, false) ;
+    }
+
+    /**
+     * Is the value 'false' (either set to the string "false" or Boolean.FALSE)
+     * or undefined
      */
-    public boolean isFalseOrUndef(Symbol property)
-    { return isFalse(property, true) ; }
-    
-    private boolean isFalse(Symbol property, boolean dft)
-    {
+    public boolean isFalseOrUndef(Symbol property) {
+        return isFalse(property, true) ;
+    }
+
+    private boolean isFalse(Symbol property, boolean dft) {
         Object x = get(property) ;
         if ( x == null )
             return dft ;
-        if ( x instanceof String )
-        {
+        if ( x instanceof String ) {
             String s = (String)x ;
-            if ( s.equalsIgnoreCase("false") ) 
+            if ( s.equalsIgnoreCase("false") )
                 return true ;
         }
         return x.equals(Boolean.FALSE) ;
     }
 
-    // -- Test for value 
+    // -- Test for value
 
     /** Test whether a named value is a specific value (.equals) */
-    
-    public boolean hasValue(Symbol property, Object value)
-    {
+
+    public boolean hasValue(Symbol property, Object value) {
         Object x = get(property) ;
         if ( x == null && value == null )
             return true ;
@@ -221,96 +275,106 @@ public class Context
     }
 
     /** Test whether a named value (as a string) has a specific string form */
-    
-    public boolean hasValueAsString(Symbol property, String value)
-    {
-        return  hasValueAsString(property, value, false) ;
+
+    public boolean hasValueAsString(Symbol property, String value) {
+        return hasValueAsString(property, value, false) ;
     }
-    
-    /** Test whether a named value (as a string) has a specific string form - can ignore
case */
-    public boolean hasValueAsString(Symbol property, String value, boolean ignoreCase)
-    {
+
+    /**
+     * Test whether a named value (as a string) has a specific string form - can
+     * ignore case
+     */
+    public boolean hasValueAsString(Symbol property, String value, boolean ignoreCase) {
         String s = getAsString(property) ;
         if ( s == null && value == null )
             return true ;
         if ( s == null || value == null )
             return false ;
-        
+
         if ( ignoreCase )
             return s.equalsIgnoreCase(value) ;
         return s.equals(value) ;
     }
-    
-    /** Set of properties (as Symbols) currently defined */  
-    public Set<Symbol> keys() { return context.keySet() ; }
 
-    /** Return the number of context items */ 
-    public int size() { return context.size() ; }
+    /** Set of properties (as Symbols) currently defined */
+    public Set<Symbol> keys() {
+        return context.keySet() ;
+    }
+
+    /** Return the number of context items */
+    public int size() {
+        return context.size() ;
+    }
+
+    // @Override
+    // public int hashCode()
+    // {
+    // return context.hashCode() ;
+    // }
+    //
+    // @Override
+    // public boolean equals(Object other)
+    // {
+    // if ( this == other ) return true ;
+    //
+    // if ( ! ( other instanceof Context ) ) return false ;
+    // Context cxt = (Context)other ;
+    // return context.equals(cxt.context) ;
+    // }
 
-    
-//    @Override
-//    public int hashCode()
-//    {
-//        return context.hashCode() ;
-//    }
-//    
-//    @Override
-//    public boolean equals(Object other)
-//    {
-//        if ( this == other ) return true ;
-//
-//        if ( ! ( other instanceof Context ) ) return false ;
-//        Context cxt = (Context)other ;
-//        return context.equals(cxt.context) ;
-//    }
-    
     // ---- Callbacks
-    public void addCallback(Callback<Symbol> m) { callbacks.add(m) ; }
-    public void removeCallback(Callback<Symbol> m) { callbacks.remove(m) ; }
-    public List<Callback<Symbol>> getCallbacks() { return callbacks ; }
-    
-    private void doCallbacks(Symbol symbol)
-    {
-        for ( Callback<Symbol> c : callbacks )
-        {
+    public void addCallback(Callback<Symbol> m) {
+        callbacks.add(m) ;
+    }
+
+    public void removeCallback(Callback<Symbol> m) {
+        callbacks.remove(m) ;
+    }
+
+    public List<Callback<Symbol>> getCallbacks() {
+        return callbacks ;
+    }
+
+    private void doCallbacks(Symbol symbol) {
+        for ( Callback<Symbol> c : callbacks ) {
             c.apply(symbol) ;
         }
     }
-    
+
     @Override
-    public String toString()
-    {
+    public String toString() {
         String x = "" ;
         String sep = "" ;
-        for ( Symbol s : keys() )
-        {
+        for ( Symbol s : keys() ) {
             Object value = get(s) ;
             x = x + sep + s + " = " + value ;
             sep = "\n" ;
         }
         return x ;
     }
-    
+
     // Put any per-dataset execution global configuration state here.
-    public static Context setupContext(Context context, DatasetGraph dataset)
-    {
+    public static Context setupContext(Context context, DatasetGraph dataset) {
         if ( context == null )
-            context = ARQ.getContext() ;    // Already copied?
+            context = ARQ.getContext() ; // Already copied?
         context = context.copy() ;
 
         if ( dataset != null && dataset.getContext() != null )
             // Copy per-dataset settings.
             context.putAll(dataset.getContext()) ;
-        
+
         context.set(ARQConstants.sysCurrentTime, NodeFactoryExtra.nowAsDateTime()) ;
-        
+
         // Allocators.
-//        context.set(ARQConstants.sysVarAllocNamed, new VarAlloc(ARQConstants.allocVarMarkerExec))
;
-//        context.set(ARQConstants.sysVarAllocAnon,  new VarAlloc(ARQConstants.allocVarAnonMarkerExec))
;
-        // Add VarAlloc for variables and bNodes (this is not the parse name). 
-        // More added later e.g. query (if there is a query), algebra form (in setOp)
-        
-        return context ; 
+        // context.set(ARQConstants.sysVarAllocNamed, new
+        // VarAlloc(ARQConstants.allocVarMarkerExec)) ;
+        // context.set(ARQConstants.sysVarAllocAnon, new
+        // VarAlloc(ARQConstants.allocVarAnonMarkerExec)) ;
+        // Add VarAlloc for variables and bNodes (this is not the parse name).
+        // More added later e.g. query (if there is a query), algebra form (in
+        // setOp)
+
+        return context ;
     }
 
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/faa8b59d/jena-arq/src/test/java/org/apache/jena/sparql/core/TestContext.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/core/TestContext.java b/jena-arq/src/test/java/org/apache/jena/sparql/core/TestContext.java
index 0be5927..5dc0e46 100644
--- a/jena-arq/src/test/java/org/apache/jena/sparql/core/TestContext.java
+++ b/jena-arq/src/test/java/org/apache/jena/sparql/core/TestContext.java
@@ -1,38 +1,40 @@
 /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
  */
 
-package org.apache.jena.sparql.core;
+package org.apache.jena.sparql.core ;
 
 import org.apache.jena.atlas.junit.BaseTest ;
+import org.apache.jena.sparql.ARQException ;
 import org.apache.jena.sparql.util.Context ;
 import org.apache.jena.sparql.util.Symbol ;
 import org.junit.Test ;
 
-public class TestContext extends BaseTest
-{
+public class TestContext extends BaseTest {
     static Symbol p1 = Symbol.create("p1") ;
     static Symbol p2 = Symbol.create("p2") ;
-    
-    @Test public void testCxt1() { Context cxt = new Context(); }
-    
-    @Test public void testCxt2()
-    { 
-        Context cxt = new Context();
+
+    @Test
+    public void testCxt1() {
+        Context cxt = new Context() ;
+    }
+
+    @Test
+    public void testCxt2() {
+        Context cxt = new Context() ;
         assertTrue("Defined in empty context", !cxt.isDefined(p1)) ;
         cxt.set(p1, "v") ;
         assertTrue("Not defined after .set", cxt.isDefined(p1)) ;
@@ -40,41 +42,67 @@ public class TestContext extends BaseTest
         assertSame("Not the same", "v", v) ;
     }
 
-    @Test public void testCxt3()
-    { 
-        Context cxt = new Context();
+    @Test
+    public void testCxt3() {
+        Context cxt = new Context() ;
         cxt.set(p1, "v") ;
         cxt.setIfUndef(p1, "w") ;
         Object v = cxt.get(p1) ;
         assertSame("Not as first set", "v", v) ;
     }
 
-    @Test public void testCxt4()
-    { 
-        Context cxt = new Context();
+    @Test
+    public void testCxt4() {
+        Context cxt = new Context() ;
         cxt.set(p1, "true") ;
         assertTrue("Not true", cxt.isTrue(p1)) ;
         assertTrue("Not true or undef", cxt.isTrueOrUndef(p1)) ;
-        
+
         assertTrue("Not false or undef", cxt.isFalseOrUndef(p2)) ;
         assertTrue("False when undef", !cxt.isFalse(p2)) ;
     }
 
-    @Test public void testCxt5()
-    { 
-        Context cxt = new Context();
+    @Test
+    public void testCxt5() {
+        Context cxt = new Context() ;
         cxt.set(p1, "false") ;
         assertTrue("Not false", cxt.isFalse(p1)) ;
         assertTrue("Not false or undef", cxt.isFalseOrUndef(p1)) ;
     }
-    
-    @Test public void testCxt6()
-    { 
-        Context cxt = new Context();
+
+    @Test
+    public void testCxt6() {
+        Context cxt = new Context() ;
         cxt.setTrue(p1) ;
         assertTrue("Not true", cxt.isTrue(p1)) ;
         String x = cxt.getAsString(p1) ;
         assertEquals("Not string 'true'", "true", x) ;
     }
 
+    @Test
+    public void testCxt7() {
+        Context cxt = new Context() ;
+        assertEquals(-1, cxt.getInt(p1, -1)) ;
+        cxt.set(p1, 1) ;
+        int x = cxt.getInt(p1, -1) ;
+        assertEquals(1, x) ;
+    }
+    
+    @Test
+    public void testCxt8() {
+        Context cxt = new Context() ;
+        assertEquals(-1L, cxt.getLong(p1, -1L)) ;
+        cxt.set(p1, 1L) ;
+        long x = cxt.getLong(p1, -2L) ;
+        assertEquals(1L, x) ;
+    }
+    
+    @Test(expected=ARQException.class)
+    public void testCxt9() {
+        Context cxt = new Context() ;
+        assertEquals(-1L, cxt.getLong(p1, -1L)) ;
+        cxt.set(p1, 1L) ;
+        // Bad. Long for Integer.
+        cxt.getInt(p1, -2) ;
+    }
 }


Mime
View raw message