-
-
Save bartdag/773367 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from py4j.java_gateway import JavaGateway | |
class Addition(object): | |
def doOperation(self, i, j): | |
return i + j | |
class Java: | |
implements = ['OperatorExample$Operator'] | |
if __name__ == '__main__': | |
gateway = JavaGateway(start_callback_server=True) | |
operator = Addition() | |
numbers = gateway.entry_point.randomBinaryOperator(operator) | |
print(numbers) | |
gateway.shutdown() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
import py4j.GatewayServer; | |
public class OperatorExample { | |
// To prevent integer overflow | |
private final static int MAX = 1000; | |
public interface Operator { | |
public int doOperation(int i, int j); | |
} | |
public List<Integer> randomBinaryOperator(Operator op) { | |
Random random = new Random(); | |
List<Integer> numbers = new ArrayList<Integer>(); | |
numbers.add(random.nextInt(MAX)); | |
numbers.add(random.nextInt(MAX)); | |
numbers.add(op.doOperation(numbers.get(0), numbers.get(1))); | |
return numbers; | |
} | |
public static void main(String[] args) { | |
GatewayServer server = new GatewayServer(new OperatorExample()); | |
server.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment