poisondog.demo.StringDemo.java Source code

Java tutorial

Introduction

Here is the source code for poisondog.demo.StringDemo.java

Source

/*
 * Copyright (C) 2015 Adam Huang <poisondog@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package poisondog.demo;

import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Queue;
import java.util.LinkedList;
import org.apache.commons.collections4.queue.CircularFifoQueue;
import java.util.Collection;

/**
 * @author Adam Huang <poisondog@gmail.com>
 */
public class StringDemo {
    public static void main(String[] args) {
        //      String temp=" command  para1 para2  ";
        //      System.out.println(temp.replaceAll("^\\s*\\S*\\s*", "").replaceAll("\\s*$", "") + "]]");

        Queue<Double> queue = new CircularFifoQueue<Double>(1000);
        List<Double> all = new LinkedList<Double>();
        List<Double> list = new ArrayList<Double>();
        double result = 0;
        double sum = 0;
        Random r = new Random();
        for (int i = 0; i < 10000; i++) {
            double g = r.nextGaussian() * 100 - 5;
            sum += g;
            if (g > 0)
                result++;
            if (i > 5000 && getRate(queue) < getRate(all) * .94)
                list.add(g);
            all.add(g);
            queue.add(g);
            //         if (queue.size() > 1000)
            //            queue.remove();

        }
        //      System.out.println(list.size());
        int count = 0;
        for (Double d : list) {
            if (d > 0)
                count++;
        }
        System.out.println("Origin Rate: " + (double) result / all.size());
        System.out.println("Origin Sum: " + sum);
        System.out.println("Upgrade Rate: " + (double) count / list.size());
        System.out.println("Upgrade Count: " + list.size());
        System.out.println("Upgrade Sum: " + getSum(list));
        System.out.println("Queue Count: " + queue.size());
    }

    private static double getSum(Iterable<Double> list) {
        double sum = 0;
        for (Double d : list) {
            sum += d;
        }
        return sum;
    }

    private static double getRate(Collection<Double> queue) {
        double good = 0;
        for (Double d : queue) {
            if (d > 0)
                good++;
        }
        return good / queue.size();
    }

    private static double getMean(Collection<Double> queue) {
        double sum = 0;
        for (Double d : queue) {
            sum += d;
        }
        return sum / queue.size();
    }
}