Skip to content

Commit

Permalink
Improved Android compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed Jan 12, 2021
1 parent f15bd8c commit 42ef661
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 22 deletions.
6 changes: 6 additions & 0 deletions pmml-evaluator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
<artifactId>commons-math3</artifactId>
</dependency>

<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-annotations</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,38 @@
*/
package org.jpmml.evaluator;

import java.util.Map;
import org.dmg.pmml.FieldName;

interface JDK8Map<K, V> extends Map<K, V> {
/**
* <p>
* Android 5.0 and 6.0 compatible implementation of {@link FieldValueMap}.
* </p>
*/
class AndroidFieldValueMap extends FieldValueMap {

AndroidFieldValueMap(){
}

AndroidFieldValueMap(int initialCapacity){
super(initialCapacity);
}

@Override
default
public V getOrDefault(Object key, V defaultValue){
public FieldValue getOrDefault(Object name, FieldValue defaultValue){

if(containsKey(key)){
return get(key);
if(containsKey(name)){
return get(name);
}

return defaultValue;
}

@Override
default
public V putIfAbsent(K key, V value){
V prevValue = get(key);
public FieldValue putIfAbsent(FieldName name, FieldValue value){
FieldValue prevValue = get(name);

if(prevValue == null){
return put(key, value);
return put(name, value);
}

return prevValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,11 @@ public class EvaluationContext {


EvaluationContext(){
this.values = new FieldValueMap();
this.values = FieldValueMap.create();
}

EvaluationContext(int numberOfVisibleFields){

if(numberOfVisibleFields <= 256){
this.values = new FieldValueMap(Math.max(2 * numberOfVisibleFields, 16));
} else

{
this.values = new FieldValueMap(numberOfVisibleFields);
}
this.values = FieldValueMap.create(numberOfVisibleFields);
}

abstract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
package org.jpmml.evaluator;

import java.util.HashMap;
import java.util.Map;

import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
import org.dmg.pmml.FieldName;

class FieldValueMap extends HashMap<FieldName, FieldValue> implements JDK8Map<FieldName, FieldValue> {
class FieldValueMap extends HashMap<FieldName, FieldValue> {

FieldValueMap(){
}
Expand All @@ -31,7 +33,61 @@ class FieldValueMap extends HashMap<FieldName, FieldValue> implements JDK8Map<Fi
super(capacity);
}

FieldValueMap(int capacity, float loadFactor){
super(capacity, loadFactor);
@Override
@IgnoreJRERequirement
public FieldValue getOrDefault(Object name, FieldValue defaultValue){
return super.getOrDefault(name, defaultValue);
}

@Override
@IgnoreJRERequirement
public FieldValue putIfAbsent(FieldName name, FieldValue value){
return super.putIfAbsent(name, value);
}

static
public FieldValueMap create(){

if(FieldValueMap.JDK8_API){
return new FieldValueMap();
}

return new AndroidFieldValueMap();
}

static
public FieldValueMap create(int numberOfVisibleFields){
int initialCapacity;

if(numberOfVisibleFields <= 256){
initialCapacity = Math.max(2 * numberOfVisibleFields, 16);
} else

{
initialCapacity = numberOfVisibleFields;
} // End if

if(FieldValueMap.JDK8_API){
return new FieldValueMap(initialCapacity);
}

return new AndroidFieldValueMap(initialCapacity);
}

protected static boolean JDK8_API;

static {
boolean jdk8_api;

try {
Map.class.getMethod("getOrDefault", Object.class, Object.class);
Map.class.getMethod("putIfAbsent", Object.class, Object.class);

jdk8_api = true;
} catch(ReflectiveOperationException roe){
jdk8_api = false;
}

JDK8_API = jdk8_api;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2021 Villu Ruusmann
*
* This file is part of JPMML-Evaluator
*
* JPMML-Evaluator is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-Evaluator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-Evaluator. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jpmml.evaluator;

import java.util.Map;

import org.dmg.pmml.FieldName;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class FieldValueMapTest {

@Test
public void jdk8() throws Exception {
// Assume that unit tests are never run in non-JDK8 compatible environment
assertTrue(FieldValueMap.JDK8_API);

assertNotNull(Map.class.getMethod("getOrDefault", Object.class, Object.class));

try {
Map.class.getMethod("getOrDefault", Object.class, FieldValue.class);

fail();
} catch(ReflectiveOperationException roe){
// Ignored
} // End try

try {
Map.class.getMethod("getOrDefault", FieldName.class, FieldValue.class);

fail();
} catch(ReflectiveOperationException roe){
// Ignored
}

assertNotNull(Map.class.getMethod("putIfAbsent", Object.class, Object.class));

try {
Map.class.getMethod("putIfAbsent", FieldName.class, FieldValue.class);

fail();
} catch(ReflectiveOperationException roe){
// Ignored
}
}
}
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@
<version>[3.1, 3.6.1]</version>
</dependency>

<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-annotations</artifactId>
<version>1.19</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down

0 comments on commit 42ef661

Please sign in to comment.