Code Examples - craterdog/java-primitive-types GitHub Wiki

This page provides simple coding examples for each of the collection classes:

The generated output for each example is also shown.

Angles

The following example code demonstrates some of the ways the Angle primitive class can be used:

    // Create some angles
    Angle pi = Angle.PI;
    Angle piOver2 = new Angle(Math.PI / 2.0d);
    Angle piOver3 = Angle.quotient(pi, 3.0d);
    Angle piOver4 = Angle.quotient(piOver2, 2.0d);
    Angle piOver6 = Angle.product(piOver3, 0.5d);

    // Do some angle calculations
    logger.info("The value of pi is: {}", pi);
    logger.info("The value of pi/2 is: {}", piOver2);
    logger.info("The value of pi/3 is: {}", piOver3);
    logger.info("The value of pi/4 is: {}", piOver4);
    logger.info("The value of pi/6 is: {}", piOver6);
    logger.info("The negative of pi/6 is: {}", Angle.negative(piOver6));
    logger.info("The inversion of pi/3 is: {}", Angle.inverse(piOver3));
    logger.info("The sum of pi/6 and pi/3 is: {}", Angle.sum(piOver6, piOver3));
    logger.info("The difference of pi/3 and pi/2 is: {}", Angle.difference(piOver3, piOver2));
    logger.info("The sine of pi/6 is: {}", Angle.sine(piOver6));
    logger.info("The cosine of pi/3 is: {}", Angle.cosine(piOver3));
    logger.info("The tangent of pi/2 is: {}", Angle.tangent(piOver2));
    logger.info("The arctangent of 1.0 is: {}", Angle.arctangent(1.0));

It will print the following output:

    The value of pi is: 3.141592653589793
    The value of pi/2 is: 1.5707963267948966
    The value of pi/3 is: 1.0471975511965976
    The value of pi/4 is: 0.7853981633974483
    The value of pi/6 is: 0.5235987755982988
    The negative of pi/6 is: -0.5235987755982988
    The inversion of pi/3 is: -2.0943951023931957
    The sum of pi/6 and pi/3 is: 1.5707963267948966
    The difference of pi/3 and pi/2 is: -0.5235987755982989
    The sine of pi/6 is: 0.49999999999999994
    The cosine of pi/3 is: 0.5000000000000001
    The tangent of pi/2 is: Infinity
    The arctangent of 1.0 is: 0.7853981633974483

Probabilities

And this example code demonstrates some of the ways that the Probability primitive class can be used:

    // Create some probabilities
    Probability always = new Probability(1.0d);
    Probability threeQuarters = new Probability(0.75d);
    Probability oneHalf = new Probability(0.5d);
    Probability oneThird = new Probability(1.0d / 3.0d);
    Probability oneQuarter = new Probability(0.75d);
    Probability never = new Probability(0.0d);

    // Do some angle calculations
    logger.info("The value of always is: {}", always);
    logger.info("The value of threeQuarters is: {}", threeQuarters);
    logger.info("The value of oneHalf is: {}", oneHalf);
    logger.info("The value of oneThird is: {}", oneThird);
    logger.info("The value of oneQuarter is: {}", oneQuarter);
    logger.info("The value of never is: {}", never);
    logger.info("The inversion of never is: {}", Probability.not(never));
    logger.info("The intersection of threeQuarters and oneThird is: {}", Probability.and(threeQuarters, oneThird));
    logger.info("The union of oneHalf and oneHalf is: {}", Probability.or(oneHalf, oneHalf));
    logger.info("The difference (sans) of oneHalf and oneHalf is: {}", Probability.sans(oneHalf, oneHalf));
    logger.info("The exclusive union (xor) of oneHalf and oneHalf is: {}", Probability.xor(oneHalf, oneHalf));
    logger.info("The results of a random coin toss are: {}", Probability.coinToss(oneHalf));

It will print the following output:

    The value of always is: 1.0
    The value of threeQuarters is: 0.75
    The value of oneHalf is: 0.5
    The value of oneThird is: 0.3333333333333333
    The value of oneQuarter is: 0.75
    The value of never is: 0.0
    The inversion of never is: 1.0
    The intersection of threeQuarters and oneThird is: 0.25
    The union of oneHalf and oneHalf is: 0.75
    The difference (sans) of oneHalf and oneHalf is: 0.25
    The exclusive union (xor) of oneHalf and oneHalf is: 0.5
    The results of a random coin toss are: true

Tags

And this code example demonstrates the ways that different size tags can be formatted:

    // Create some tags
    Tag small = new Tag(2);
    Tag medium = new Tag(8);
    Tag large = new Tag();  // defaults to 20 bytes
    Tag huge = new Tag(128);

    // Display the tags
    logger.info("The default format of a random 2 byte tag is: {}", small);
    logger.info("The default format of a random 8 byte tag is: {}", medium);
    logger.info("The default format of a random 20 byte tag is: {}", large);
    logger.info("The default format of a random 128 byte tag is: {}", huge);

It will print the following output:

    The default format of a random 2 byte tag is: QHP0
    The default format of a random 8 byte tag is: JDPFKG8AN4PS8
    The default format of a random 20 byte tag is: PJMMDBFZJCHPVFYYYY2MJ8C2J76W3S1Y
    The default format of a random 128 byte tag is: R2LXSXRCVG9FL87MGBYA7TMH6RBTTC95M58SPBLYQZ01QN99GTVW989NMH4QZH9RW4CDHNP81CSFTLFYVZWM5FRHC5J9WD6Q1H3JHQ9XXL00RVW95731MMK7Z601QXDNS43B0A5G3ALLY6SKMKD0XZAVRKGW10T4C00FZNLZMPFBXNACMH5QXTGQ16LLNYX98QGXG5VDF41ZM

TextStrings

And this code example demonstrates how a text string can be iterated over:

    // Create some text strings
    TextString emptyText = new TextString();  // empty text string
    assert emptyText.isEmpty();
    TextString shortText = new TextString("abcde");
    assert shortText.getSize() == 5;

    // Iterate through the characters
    for (Character c : shortText) {
        logger.info("character: '{}'", c);
    }

It will print the following output:

    character: 'a'
    character: 'b'
    character: 'c'
    character: 'd'
    character: 'e'

BinaryStrings

And finally, this code example demonstrates how a binary string can be formatted:

    // Create a binary string
    BinaryString randomBinary = new BinaryString(RandomUtils.generateRandomBytes(100));
    assert randomBinary.getSize() == 100;

    // Format the bytes using various bases
    logger.info("The binary string in base 2 is: {}\n", randomBinary.toString(2, "  "));
    logger.info("The binary string in base 16 is: {}\n", randomBinary.toString(16, "  "));
    logger.info("The binary string in base 32 is: {}\n", randomBinary.toString(32, "  "));
    logger.info("The binary string in base 64 is: {}\n", randomBinary.toString(64, "  "));

It will print the following output:

The binary string in base 2 is: 
  10111011011100111000111000000100010011101001011011100000010000000001101010110101
  01111010111100001010000000000001101011010011001010111000000010100110110001111010
  01111111001111000100101000100101101101110001100001011101011111100010000000010001
  11011011011101000111111001100100000110110100110000110101111111010110101110011101
  00110110000001000110111010101100010010010010011100011010101010001111110001100110
  11111101100110011100010111000010000011000110111011101110100010110101001101111101
  10111010111000110011011111110001011111001110101111010101111010000100000101110111
  11100110011000100100000010100110010101101110100111010010101001101101101110001010
  01100111001000001011001010111000110011001100001100000101101111011111000110000000
  01100100001011000011011000001101111011010000100101110100110000111100010010101100

The binary string in base 16 is: 
  BB738E044E96E0401AB57AF0A001AD32B80A6C7A7F3C4A25B7185D7E2011DB747E641B4C35FD6B9D
  36046EAC49271AA8FC66FD99C5C20C6EEE8B537DBAE337F17CEBD5E84177E66240A656E9D2A6DB8A
  6720B2B8CCC305BDF180642C360DED0974C3C4AC

The binary string in base 32 is: 
  QDSRW12FKVH406NNGBRA00DD6AW0MV3TGWY4M9DQ31FQW80JVDT7WS0V9HTZTTWX6R26XB294WDAJZ36
  ZPCWBHHCDVQ8PMVXQBJLGWBWXGAYHHBQWSJ419KPX79ADPWACWHB5F6CRC2VVWC0CHP3C3GD15TC7J5C

The binary string in base 64 is: 
  u3OOBE6W4EAatXrwoAGtMrgKbHp/PEoltxhdfiAR23R+ZBtMNf1rnTYEbqxJJxqo/Gb9mcXCDG7ui1N9
  uuM38Xzr1ehBd+ZiQKZW6dKm24pnILK4zMMFvfGAZCw2De0JdMPErA==