javaembed_ex11.ecl 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2019 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. //class=embedded
  14. //class=3rdparty
  15. IMPORT Java;
  16. UNSIGNED JavaAccumulator(INTEGER initial) := EMBED(Java)
  17. public class JavaAccumulator
  18. {
  19. public JavaAccumulator(int initial) { tot = initial; }
  20. public synchronized int accumulate(int a)
  21. {
  22. tot = tot + a;
  23. return tot;
  24. }
  25. public synchronized int clear()
  26. {
  27. int _tot = tot;
  28. tot = 0;
  29. return _tot;
  30. }
  31. private int tot = 0;
  32. }
  33. ENDEMBED;
  34. INTEGER accumulate(UNSIGNED p, INTEGER val) := IMPORT(Java, 'JavaAccumulator::accumulate');
  35. INTEGER clear(UNSIGNED p) := IMPORT(Java, 'JavaAccumulator::clear');
  36. release(UNSIGNED p) := IMPORT(Java, '~JavaAccumulator'); // After calling this the java object p is no longer usable
  37. a := JavaAccumulator(35) : INDEPENDENT;
  38. ORDERED
  39. (
  40. accumulate(a, 1);
  41. accumulate(a, 2);
  42. accumulate(a, 3);
  43. clear(a);
  44. accumulate(a, 10);
  45. release(a);
  46. );