コード例 #1
1
ファイル: Flips.java プロジェクト: wusung/algorithm-example
 public static void main(String[] args) {
   int T = Integer.valueOf(args[0]);
   Counter heads = new Counter("heads");
   Counter tails = new Counter("tails");
   for (int i = 0; i < T; i++) {
     if (StdRandom.bernoulli(0.5)) {
       heads.increment();
     } else {
       tails.increment();
     }
   }
   StdOut.println(heads);
   StdOut.println(tails);
   int d = heads.tally() - tails.tally();
   StdOut.println("delta:" + java.lang.Math.abs(d));
 }
コード例 #2
0
    public IotHubMessageResult execute(Message msg, Object context) {
      Counter counter = (Counter) context;
      System.out.println(
          "Received message "
              + counter.toString()
              + " with content: "
              + new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET));

      int switchVal = counter.get() % 3;
      IotHubMessageResult res;
      switch (switchVal) {
        case 0:
          res = IotHubMessageResult.COMPLETE;
          break;
        case 1:
          res = IotHubMessageResult.ABANDON;
          break;
        case 2:
          res = IotHubMessageResult.REJECT;
          break;
        default:
          // should never happen.
          throw new IllegalStateException("Invalid message result specified.");
      }

      System.out.println("Responding to message " + counter.toString() + " with " + res.name());

      counter.increment();

      return res;
    }
コード例 #3
0
ファイル: Interval2D.java プロジェクト: akiuesltd/src_root
  /** Unit tests the <tt>com.akieus.algos.coursera.lib.Interval2D</tt> data type. */
  public static void main(String[] args) {
    double xlo = Double.parseDouble(args[0]);
    double xhi = Double.parseDouble(args[1]);
    double ylo = Double.parseDouble(args[2]);
    double yhi = Double.parseDouble(args[3]);
    int T = Integer.parseInt(args[4]);

    Interval1D xinterval = new Interval1D(xlo, xhi);
    Interval1D yinterval = new Interval1D(ylo, yhi);
    Interval2D box = new Interval2D(xinterval, yinterval);
    box.draw();

    Counter counter = new Counter("hits");
    for (int t = 0; t < T; t++) {
      double x = StdRandom.uniform(0.0, 1.0);
      double y = StdRandom.uniform(0.0, 1.0);
      Point2D p = new Point2D(x, y);

      if (box.contains(p)) counter.increment();
      else p.draw();
    }

    StdOut.println(counter);
    StdOut.printf("box area = %.2f\n", box.area());
  }
コード例 #4
0
 public void addValue(int value) {
   Object count = counts.get(value);
   if (count == null) {
     count = new Counter();
     counts.put(value, count);
   }
   ((Counter) count).increment();
   total++;
 }
コード例 #5
0
ファイル: Worker.java プロジェクト: spatialsmart/matsim
 private void addEvent(Event event) {
   this.numEvents++;
   String lexString = toLexicographicSortedString(event);
   Counter counter = this.events.get(lexString);
   if (counter == null) {
     counter = new Counter();
     this.events.put(lexString, counter);
   }
   counter.increment();
 }
コード例 #6
0
ファイル: ColorCounter.java プロジェクト: moink635/mysource
 public void addColor(final HSBColor color) {
   if (!color.hasTransparency()) {
     Counter counter = colors.get(color);
     if (counter == null) {
       counter = new Counter();
       colors.put(color, counter);
     }
     counter.increment();
   }
 }
コード例 #7
0
    public IotHubMessageResult execute(Message msg, Object context) {
      Counter counter = (Counter) context;
      System.out.println(
          "Received message "
              + counter.toString()
              + " with content: "
              + new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET));

      counter.increment();

      return IotHubMessageResult.COMPLETE;
    }
コード例 #8
0
ファイル: CounterTest.java プロジェクト: rmorris144/ctec2602
  public static void main(String[] args) {
    /* The following test code is just an example of how you can write
     * a simple test program to check the basic functionality of your class
     * works as you expect. The primary aim is to test each constructor and
     * method - there are many combinations of how you could go about this.
     * For more complex classes and/or methods you may write several different
     * tests for each method to check boundary cases for example.
     */

    System.out.println("Testing default constructor initialisation and get method...");
    Counter c = new Counter();
    System.out.println("Expected 0: Actual: " + c.getCount());

    System.out.println("Testing increment method...");
    c.increment();
    c.increment();
    System.out.println("Expected 2: Actual: " + c.getCount());

    System.out.println("Testing set method...");
    c.setCount(10);
    System.out.println("Expected 10: Actual: " + c.getCount());

    System.out.println("Testing custom constructor initialisation...");
    Counter c1 =
        new Counter(
            5); // Note: creating a second object instance of the Counter class (which has its own
                // state)
    System.out.println("Expected 5: Actual: " + c1.getCount());

    System.out.println("Testing reset method...");
    c1.reset();
    System.out.println("Expected 0: Actual: " + c1.getCount());

    System.out.println("Testing toString method...");
    System.out.println("Expected count=0: Actual: " + c1.toString());

    System.out.println("Testing decrement method..");
    c.decrement();
    c.decrement();
    System.out.println("Expected 1: Actual:" + c.getCount());

    Counter launch = new Counter();

    launch.incremendBy(10);

    while (!launch.isZero()) {
      System.out.print(launch.getCount() + " ");
      launch.decrement();
    }

    System.out.print("Blast off!");
  }
コード例 #9
0
  public void testNumber() {
    Counter ct = CounterFactory.createCounter("testNumber", Type.NUMBER);

    assertEquals("Default value", 0.0, ct.nextValue(), delta);
    ct.setRawValue(10.0);
    assertEquals("setRawValue", 10.0, ct.nextValue(), delta);
    ct.increment();
    assertEquals("increment", 11.0, ct.nextValue(), delta);
    ct.incrementBy(2.0);
    assertEquals("incrementBy", 13.0, ct.nextValue(), delta);
    ct.decrement();
    assertEquals("decrement", 12.0, ct.nextValue(), delta);
  }
コード例 #10
0
 public static void main(String[] args) {
   System.out.println("This programs tests the features of the counter class.");
   Counter c1 = new Counter(), c2 = new Counter();
   tester(c1, c2);
   c2.decrement();
   tester(c1, c2);
   c1.setCounter(0);
   c2.increment();
   tester(c1, c2);
   c2.setCounter(c1.getCounter());
   tester(c1, c2);
   c1.decrement();
   tester(c1, c2);
   System.out.println("Test complete.");
 }
コード例 #11
0
ファイル: ColorCounter.java プロジェクト: moink635/mysource
  /** Like {#getColorRatio}, except it groups similar colours together. */
  public List<ColorCount> getSimilarColorRatio() {
    final List<ColorCount> colorRatio = getColorRatio();
    Map<HSBColor, Counter> similarColors = new HashMap<HSBColor, Counter>();

    for (final ColorCount colorCount : colorRatio) {
      HSBColor similarColor = findSimilarColor(colorCount.getColor(), similarColors);
      Counter counter;
      if (similarColor != null) {
        counter = similarColors.get(similarColor);
      } else {
        // No similar color found
        counter = new Counter();
        similarColors.put(colorCount.getColor(), counter);
      }

      counter.increment(colorCount.getCount());
    }

    return convertMapToList(similarColors);
  }
コード例 #12
0
  @Override
  public void visitToken(DetailAST ast) {
    final int tokenType = ast.getType();

    if (tokenType == TokenTypes.CLASS_DEF
        || tokenType == TokenTypes.METHOD_DEF
        || tokenType == TokenTypes.CTOR_DEF
        || tokenType == TokenTypes.STATIC_INIT
        || tokenType == TokenTypes.INSTANCE_INIT) {
      // add a counter for this class/method
      counters.push(new Counter());
    }

    // check if token is countable
    if (isCountable(ast)) {
      // increment the stacked counters
      for (final Counter counter : counters) {
        counter.increment();
      }
    }
  }
コード例 #13
0
  public void increment(long time) {
    super.increment();

    counter.addAndGet(time);
  }
コード例 #14
0
 public void run() {
   for (int i = 0; i < 1000000; i++) Counter.increment();
 }
コード例 #15
0
  protected void revisionToInstance() {
    synchronized (recursionCounter) {
      if (underConstruction) {
        // Return if revisionToInstance was called before to avoid doubled calls
        return;
      }

      underConstruction = true;
      InternalCDORevision revision = cdoRevision();

      if (TRACER.isEnabled()) {
        TRACER.format(
            "Transfering revision to instance: {0} --> {1}", revision, instance); // $NON-NLS-1$
      }

      boolean deliver = instance.eDeliver();
      if (deliver) {
        instance.eSetDeliver(false);
      }

      Counter counter = recursionCounter.get();
      if (counter == null) {
        counter = new Counter();
        recursionCounter.set(counter);
      }

      InternalCDOResource resource = null;
      boolean bypassPermissionChecks = revision.bypassPermissionChecks(true);

      try {
        registerWrapper(this);
        counter.increment();
        viewAndState.view.registerObject(this);

        revisionToInstanceResource();
        revisionToInstanceContainer();

        Resource eResource = instance.eResource();
        if (eResource instanceof InternalCDOResource) {
          resource = (InternalCDOResource) eResource;
          resource.cdoInternalLoading(instance);
        }

        for (EStructuralFeature feature : classInfo.getAllPersistentFeatures()) {
          revisionToInstanceFeature(feature);
        }
      } catch (RuntimeException ex) {
        OM.LOG.error(ex);
        throw ex;
      } catch (Exception ex) {
        OM.LOG.error(ex);
        throw new CDOException(ex);
      } finally {
        try {
          revision.bypassPermissionChecks(bypassPermissionChecks);

          if (resource != null) {
            resource.cdoInternalLoadingDone(instance);
          }

          if (deliver) {
            instance.eSetDeliver(true);
          }
        } finally {
          if (counter.decrement() == 0) {
            recursionCounter.remove();
          }

          unregisterWrapper(this);
          underConstruction = false;
        }
      }
    }
  }