/** * Executes example of calculation of the greatest common divisor (GCD) and the lowest common * multiple (LCM) for each generated pair of integers using new functional APIs. * * @param args Command line arguments, none required but if provided first one should point to the * Spring XML configuration file. See {@code "examples/config/"} for configuration file * examples. * @throws GridException If example execution failed. */ public static void main(String[] args) throws Exception { // Typedefs: // --------- // G -> GridFactory // CI1 -> GridInClosure // CO -> GridOutClosure // CA -> GridAbsClosure // F -> GridFunc G.in( args.length == 0 ? null : args[0], new CIX1<Grid>() { @Override public void applyx(Grid g) throws GridException { // Bound for random numbers. int bound = 100; // Collection size. int size = 10; // Initialises collection of pair random numbers. Collection<int[]> pairs = new ArrayList<int[]>(size); // Fills collection. for (int i = 0; i < size; i++) { pairs.add( new int[] { GridNumberUtilExample.getRand(bound), GridNumberUtilExample.getRand(bound) }); } // Calculates and prints GCD and LCM for each pair of numbers with closure. g.run( SPREAD, F.yield( pairs, new CI1<int[]>() { @Override public void apply(int[] pair) { int gcd = GridNumberUtilExample.getGCD(pair[0], pair[1]); int lcm = GridNumberUtilExample.getLCM(pair[0], pair[1]); System.out.printf( ">>>>> Numbers: %d and %d. GCD: %d. LCM: %d.%n", pair[0], pair[1], gcd, lcm); } })); // Prints. X.println(">>>>> Check all nodes for numbers and their GCD and LCM output."); } }); }
/** * Executes information gathering example with closures. * * @param args Command line arguments, none required but if provided first one should point to the * Spring XML configuration file. See {@code "examples/config/"} for configuration file * examples. * @throws GridException If example execution failed. */ public static void main(String[] args) throws Exception { // Typedefs: // --------- // G -> GridFactory // CI1 -> GridInClosure // CO -> GridOutClosure // CA -> GridAbsClosure // F -> GridFunc G.in( args.length == 0 ? null : args[0], new CIX1<Grid>() { @Override public void applyx(Grid g) throws GridException { // Broadcast closure to all nodes for gathering their system information. String res = g.reduce( GridClosureCallMode.BROADCAST, Collections.<GridOutClosure<String>>singleton( new CO<String>() { @Override public String apply() { StringBuilder buf = new StringBuilder(); buf.append("OS: ") .append(System.getProperty("os.name")) .append(" ") .append(System.getProperty("os.version")) .append(" ") .append(System.getProperty("os.arch")) .append("\nUser: "******"user.name")) .append("\nJRE: ") .append(System.getProperty("java.runtime.name")) .append(" ") .append(System.getProperty("java.runtime.version")); return buf.toString(); } }), new R1<String, String>() { private StringBuilder buf = new StringBuilder(); @Override public boolean collect(String s) { buf.append("\n").append(s).append("\n"); return true; } @Override public String apply() { return buf.toString(); } }); // Print result. X.println("Nodes system information:"); X.println(res); } }); }