r/javaexamples • • Mar 04 '23

REST to gRPC | Suggestions

1 Upvotes

I have taken it upon my self to migrate a Micro-Service I look after to gRPC. My manager is open to the idea of experimentation.

After doing a lot of research and learning basics about it, first thing which is preventing me from going ahead is there is no official library/project from spring from this. All I have found over and over again are reference to this github project [GitHub - yidongnan/grpc-spring-boot-starter: Spring Boot starter module for gRPC framework.] which seem fairly active.

I am fairly new as SE with only one 1 YOE. I have few concerns though. First one being is it a good idea to use aforementioned project for production? If so are there any articles/guides/case studies that you would like to refer?

I am open to honest thoughts, suggestions and criticism. FYI, org I work in is industry leading and gets massive traffic. However, Mircro-Service I am working on can handle 5-10 minutes of downtime as worse without really impacting business because of the fallbacks.


r/javaexamples • • Feb 05 '23

11 Examples of LocalDate, LocalTime, and LocalDateTime in Java 8

1 Upvotes

r/javaexamples • • Jan 31 '23

3 Examples of Anonymous Class to Learn Lambda Expression better in Java 8

1 Upvotes

r/javaexamples • • Jan 28 '23

Map.compute(), computeIfPresent() and ComputeIfAbsent Example in Java

1 Upvotes

r/javaexamples • • Jan 07 '23

How to write to a File with try-with-resource in Java? Example Tutorial

1 Upvotes

r/javaexamples • • Jul 19 '22

Checking if object Exists in list

1 Upvotes

What could I be doing wrong. Basically looking to search through a folder of files to see if the file entered by a user to see if the file exists. My if statement is returning the opposite of what I am expecting. Showing that the file does not exist when it does.

import java.io.File;

import java.util.Scanner;

public class Main {

public static void main(String\[\] args) {
	doesFileExist();
}
public static void doesFileExist() {		

	Scanner scan = new Scanner([System.in](https://System.in));
	System.out.print("Enter filename to be searched:");

	String userFilename = scan.nextLine();
	System.out.print("Enter path where file resides:");

	String userFilePath = scan.nextLine();
	File\[\] fl = new File("userFilePath").listFiles();
	if (fl != null) {

		System.out.println("List is not empty");

	} else {

		System.out.println("Empty List");

	}
}

}


r/javaexamples • • May 25 '22

Question for the Day: Please provide your answers in the comments below.

1 Upvotes

Suppose we have the following class in the file

/my/directory/named/A/Bird.java.

Which of the answer options replaces INSERT CODE HERE when added independently if we compile from /my/directory? (Choose all that apply.)

  1. INSERT CODE HERE

public class Bird { }

a) package my.directory.named.a;

b) package my.directory.named.A;

c) package named.a;

d) package named.A;

e) package a;

f) package A;


r/javaexamples • • May 16 '22

Question for the Day: #7 (Please provide your answers in the comments below)

1 Upvotes

Given the following class, which of the following calls print out Blue Jay? (Choose all that apply.)

public class BirdDisplay {

public static void main(String[] name) {

System.out.println(name[1]);

} }

a. java BirdDisplay Sparrow Blue Jay

b. java BirdDisplay Sparrow "Blue Jay"

c. java BirdDisplay Blue Jay Sparrow

d. java BirdDisplay "Blue Jay" Sparrow

e. java BirdDisplay.class Sparrow "Blue Jay"

f. java BirdDisplay.class "Blue Jay" Sparrow


r/javaexamples • • May 13 '22

How to create and use a jar file in Eclipse

1 Upvotes

A video on Creating and using a jar file in Eclipse is OUT NOW!!!! You can checkout the video here.


r/javaexamples • • May 13 '22

Question for the Day: #6 (Please provide your answers in the comments section below)

1 Upvotes

Given the following classes, which of the following snippets can independently be

inserted in place of INSERT IMPORTS HERE and have the code compile?

(Choose all that apply.)

package aquarium;

public class Water {

boolean salty = false;

}

package aquarium.jellies;

public class Water {

boolean salty = true;

}

package employee;

INSERT IMPORTS HERE

public class WaterFiller {

Water water;

}

  1. import aquarium.*;

  2. import aquarium.Water;

  3. import aquarium.*;

  4. import aquarium.*;

  5. import aquarium.Water;

  6. None of these imports can make the code compile.


r/javaexamples • • May 13 '22

How to Convert a Stream to List, Set, and Map in Java? Example Tutorial

1 Upvotes

r/javaexamples • • Dec 19 '20

UST Global Java Virtual Interview | Part2 | Sync Tech

1 Upvotes

Java virtual interview


r/javaexamples • • Sep 14 '20

How to decompile .jar file or .class file? | Java Decompilers & eclipse ...

1 Upvotes

r/javaexamples • • Jul 30 '19

Top 5 Frameworks for Java Developers

1 Upvotes

Java is one of the widely used programming languages. There are many useful Java frameworks available to write any Java-based web and mobile apps, REST APIs and Microservices easily.

In this article we will see Top 5 Frameworks for Java Developers.

https://www.opencodez.com/java/top-5-frameworks-java-developers.htm


r/javaexamples • • Jul 17 '19

A Simple Websocket Application in SpringBoot

1 Upvotes

Websocket is used to develop two way communication between server and browser. For example, in a chat application, if a user sends a message to a different user, the the application sends that message to the server first and the server informs the application of the second user . If we don’t use websocket, the application or the client will have to pull the server data continuously for any new updates.

In Sprint Boot, we can easily create a websocket application. In this tutorial, I will show you how to create a websocket server in Spring boot. We will learn how to connect and disconnect from a socket server and how to send a simple text message. The client will subscribe to the server, it will accept a string and push a separate string to the subscriber client.

https://www.opencodez.com/java/simple-websocket-application-springboot.htm


r/javaexamples • • Jun 27 '19

Java Creational Design Patterns – Singleton Pattern

1 Upvotes

r/javaexamples • • Jun 05 '19

Java Collections - NavigableSet

1 Upvotes

Java Collections - NavigableSet with code Examples

Java NavigableSet


r/javaexamples • • May 25 '19

Java Collection - Deque

1 Upvotes

Java collection - Deque with code examples Java Deque


r/javaexamples • • May 09 '19

Java Control Flow

1 Upvotes

Java Control statement - If-else | Switch | Loops

Java Control Statement


r/javaexamples • • May 07 '19

Java concurrent API - BlockingQueue

1 Upvotes

Java concurrent API - BlockingQueue With solution to Producers-Consumers problem

Blocking Queue


r/javaexamples • • Jan 29 '19

Super Keyword in Java | Example & Programs

1 Upvotes

r/javaexamples • • Jun 01 '18

my java swing tuts

1 Upvotes

r/javaexamples • • Aug 22 '17

Pseudo Random Number Generators: The Mersenne Twister and more

1 Upvotes

Psuedo Random Number Generators

Since computers cannot generate truly 'random' numbers, they have to simulate the randomness using mathematical formulas. Java's built in Random class uses a simple one called a Linear Congruential Generator, which can be considered outdated when compared with newer algorithms.

We're going to take a look at a couple other algorithms. Note that none of these are cryptographically secure random algorithms like Java's SecureRandom, and so should not be used for any case where security is important. (i.e. generating encryption keys, online casino games, etc.)

Factory Pattern

We will use a Factory pattern so that we can set up different algorithms and easily swap them out during testing. So before we get into that we need to set up some abstractions:

First, an interface, defining the public methods to be used by all:

public interface AltRandom {
    int nextInt();
    int nextInt(int n);
    int nextInt(int min, int max);
    long nextLong();
    double nextDouble();
    boolean nextBoolean();
    float nextFloat();
    void nextBytes(byte[] bytes);
}

Now we need an abstract class:

public abstract class AbstractRandom implements AltRandom {
    protected long seed;


    protected abstract void setSeed(long seed);
    protected abstract long generateSeed();
    protected abstract long next();

All of the random algorithms need a seed value, to initialize the state of the PNRG. The seed can either be specified by the user, or generated by the algorithm. All of the algorithms also need a next() method as the main internal method. More will be added to this abstract class as you will see later.

So, now we make an enum to hold the different available algorithms we will implement:

public enum Algorithm {    
    MERSENNE_TWISTER,
    XOR_SHIFT,
    XORO_SHIFT,
    SPLIT_MIX
}

And then our Factory, which returns to the user a new instance of the algorithm for the given enum, with the given seed or with a generated seed:

public class RandomFactory {

    /**
     * returns instance of Alternate Random Generator based on enumerated value
     * initializes generator with given seed, using object Long value so it can be nullable.
     * @param algo algorithm type from enum
     * @param seed value to seed PNRG
     * @return instance of interface AltRandom
     */
    public static AltRandom getInstance(Algorithm algo, Long seed) {
        switch (algo) {
            case MERSENNE_TWISTER:
                if (seed != null) {
                    return new MersenneTwister(seed);
                }
                return new MersenneTwister();
            case XOR_SHIFT:
                if (seed != null) {
                    return new XorShift128Plus(seed);
                }
                return new XorShift128Plus();
            case XORO_SHIFT:
                if (seed != null) {
                    return new XoroShift128Plus(seed);
                }
                return new XoroShift128Plus();
            case SPLIT_MIX:
                if (seed != null) {
                    return new SplitMix64(seed);
                }
                return new SplitMix64();
        }
        return null;
    }

    /**
     * returns instance of Alt Random matching the enumerated algorithm
     * uses generated seed
     * @param algo algorithm type from enum
     * @return instance of interface AltRandom
     */
    public static AltRandom getInstance(Algorithm algo) {
        return getInstance(algo, null);
    }
}

The Mersenne Twister

Another popular PRNG algorithm is the Mersenne Twister. Now, I'm not going to even pretend i understand the math behind it, but basically you create an array of 624 integers, initializing them using the given seed and then performing multiplication with a magic number, and a series of XOR and bitshift operations. This array is refilled every time you get to the last number. each number, as it is extracted from the array, is also tempered through a series of XORing and bit shifting.

public class MersenneTwister extends AbstractRandom {

first, the large set of magic numbers needed for the algorithm:

    private final int WORD = 32;
    private final int N = 624;
    private final int MIDDLE = 397;
    private final int A = 0x9908B0DF;
    private final int U = 11;
    private final int S = 7;
    private final int T = 37;
    private final int L = 18;
    private final int B = 0x9D2C5680;
    private final int C = 0xEFC60000;
    private final int F = 0x6C078965;
    private final int UPPER_MASK = 0x80000000;
    private final int LOWER_MASK = 0x7FFFFFFF;

An array to hold our state, and an index to keep track of where we are in the array:

    private int[] mt = new int[N];
    private int index;

Constructors:

    public MersenneTwister() {
        super.seed = generateSeed();
        setSeed(super.seed);
    }

    public MersenneTwister(long seed) {
        setSeed(seed);
    }

If no seed given, use system time:

    @Override
    protected long generateSeed() {
       return System.currentTimeMillis() ^ System.nanoTime();
    }

Now we initialize our state array using the seed and a series or XOR and shifts:

    @Override
    protected void setSeed(long seed) {
        index = N;
        mt[0] = (int) seed;
        for (int i = 1; i < N; i++) {
            mt[i] = (F * (mt[i - 1] ^ (mt[i - 1] >> (WORD - 2))) + i);
        }
    }

Get the next long value. Use a series of XOR and shifting with the magic numbers.

    @Override
    protected long next() {
        if (index >= N) {
            twist();
        }

        long y = mt[index++];
        y ^= (y >> U);
        y ^= (y << S) & B;
        y ^= (y << T) & C;
        y ^= (y >> L);

        return y;
    }

If at the end of array, perform 'twist' to repopulate the values. Don't ask.

    private void twist() {
        for (int i = 0; i < N; i++) {
            int x = (mt[i] & UPPER_MASK) + (mt[(i+1) % N] & LOWER_MASK);
            int x_shifted = x >> 1;
            if (x % 2 != 0) {
                x_shifted ^= A;
            }
            mt[i] = mt[(i + MIDDLE) % N] ^ x_shifted;
        }
        index = 0;
    }


}

Note that we have not implemented any of the methods from the AltRandom interface yet. The reason is, we are going to implement them in the abstract parent class, as they will be the same for most of the algorithms, with a couple exceptions. Also note that this is the 32-bit implementation of the Mersenne Twister, yet we are returning a long value with our next. You will see this is actually unimportant.

So, let's go back to our AbstractRandom class and implement the public methods: (Most of these are modeled after java.util.Random)

protected int next(int bits){
    if (bits < 0 || bits > 32) {
        throw new IllegalArgumentException("bits parameter out of range (0 - 32");
    }
    return (int) next() >>> (32 - bits);
}

@Override
public int nextInt() {
    return next(32);
}

@Override
public int nextInt(int n) {
    if (n <= 0) {
        throw new IllegalArgumentException("Range value must be non-zero");
    }
    return next(31) % n;
}

Why oh why doesn't java.util.Random have this one!!!

@Override
public int nextInt(int min, int max) {
    if (max <= min) {
        throw new IllegalArgumentException("min must be smaller than max.");
    }
    return nextInt((max - min) + 1) + min;
}

Note for nextLong we take two next ints and OR them together

@Override
public long nextLong() {
    return ((long)(next(32)) << 32) + next(32);
}

@Override
public double nextDouble() {
    return (((long)(next(26)) << 27) + next(27))
            / (double)(1L << 53);
}

@Override
public boolean nextBoolean() {
    return next(1) != 0;
}

@Override
public float nextFloat() {
    return next(24) / ((float) (1 << 24));
}

@Override
public void nextBytes(byte[] bytes) {
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = (byte) next(8);
    }
}

Other PNRGs

Arguments can be made against the use of the Mersenne Twister algorithm, mostly about the amount of memory used and speed. More recent advancements show that the PNRG can have improved efficiency with smaller amounts of state. The XORSHIFT algorithm uses only 128 bits of state.

XOR SHIFT 128 Plus

Based on here

public class XorShift128Plus extends AbstractRandom {
    private long[] s = new long[2];

Note this used yet another PNRG, the SplitMix 64 which will be described later, is used to generate a seed of multiple values

    public XorShift128Plus() {
        SplitMix64 genSeed = new SplitMix64();
        s[0] = genSeed.next();
        s[1] = genSeed.next();
        this.seed = 0;
    }

    public XorShift128Plus(long seed) {
        setSeed(seed);
    }

    @Override
    protected void setSeed(long seed) {
        SplitMix64 genSeed = new SplitMix64(seed);
        s[0] = genSeed.next();
        s[1] = genSeed.next();
        this.seed = seed;
    }

This method is not necessary here, override

    @Override
    protected long generateSeed() {
        return 0;
    }

Uses a series of XORing and bit shifts to randomize the state

    @Override
    protected long next() {
        long s1 = s[0];
        long s0 = s[1];
        long result = s0 + s1;
        s[0] = s0;
        s1 ^= s1 << 23;
        s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5);
        return result;
    }

Since this is a true 64-bit generator, next long can just be next()

    @Override
    public long nextLong() {
        return next();
    }
}

XORO Shift 128 Plus Another improvement on that algorithm, from here

public class XoroShift128Plus extends AbstractRandom {
    private long[] s = new long[2];

    public XoroShift128Plus() {
        SplitMix64 genSeed = new SplitMix64();
        s[0] = genSeed.next();
        s[1] = genSeed.next();
        this.seed = 0;
    }

    public XoroShift128Plus(long seed) {
        setSeed(seed);
    }
    @Override
    protected void setSeed(long seed) {
        SplitMix64 genSeed = new SplitMix64(seed);
        s[0] = genSeed.next();
        s[1] = genSeed.next();
        this.seed = seed;
    }

    @Override
    protected long generateSeed() {
        return 0;
    }

This one uses bit rotation as well as the XOR and shift

    @Override
    protected long next() {
        long s0 = s[0];
        long s1 = s[1];
        long result = s0 + s1;

        s1 ^= s0;
        s[0] = Long.rotateLeft(s0, 55) ^ s1 ^ (s1 << 14);
        s[1] = Long.rotateLeft(s1, 36);
        return result;
    }

    @Override
    public long nextLong() {
        return next();
    }

It is suggested to use the sign bit for boolean instead of the lowest bit

    // use sign bit for boolean as suggested
    @Override
    public boolean nextBoolean() {
        return ((next() >> 63) & 1) == 1;
    }
}

SPLIT MIX 64 This algorithm is used by Java's ThreadLocalRandom and we use it here as recommended to generate the state for the XOR-shift based algorithms:

public class SplitMix64 extends AbstractRandom {
    private final long SPLITMIX64_A = 0x9E3779B97F4A7C15L;
    private final long SPLITMIX64_B = 0xBF58476D1CE4E5B9L;
    private final long SPLITMIX64_C = 0x94D049BB133111EBL;
    private long smix;

    public SplitMix64() {
        setSeed(generateSeed());
    }

    public SplitMix64(long seed) {
        setSeed(seed);
    }

    @Override
    protected void setSeed(long seed) {
        this.seed = seed;
        smix = seed;
    }

    @Override
    protected long generateSeed() {
        this.seed = System.currentTimeMillis() ^ System.nanoTime();
        return seed;
    }

    @Override
    protected long next() {
        smix += SPLITMIX64_A;
        long z = smix;
        z = (z ^ (z >> 30)) * SPLITMIX64_B;
        z = (z ^ (z >> 27)) * SPLITMIX64_C;
        return z ^ (z >> 31);
    }

    @Override
    public long nextLong() {
        return next();
    }    
}

So now, for usage, you initialize like this:

    AltRandom random = RandomFactory.getInstance(Algorithm.MERSENNE_TWISTER);

And change the Algorithm enum to select the desired one. Then use just like java.util.Random.

Testing

There are comprehensive tests for PNRGs, here I will just run a couple very basic tests.

First, lets test speed. I will run 10 tests of generating 1 million random ints, time each run, and then throw out the slowest and fastest times and get the average:

     AltRandom random = RandomFactory.getInstance(Algorithm.MERSENNE_TWISTER);

     List<Integer> tests = new ArrayList<>();

     for (int i = 0; i < 10; i++) {
         start = System.currentTimeMillis();
         for (int j = 0; j < 1000000; j++) {
             int r = random.nextInt();
         }
         tests.add((int) (System.currentTimeMillis() - start));
     }


     double averageTime = tests.stream()
             .sorted()
             .limit(tests.size() - 1)
             .skip(1)
             .mapToInt(x -> x)
             .average()
             .getAsDouble();

     System.out.println("Average Time elapsed: " + averageTime);

Next we will test for even distribution. We will run 1 million random ints from 0 - 99, make a frequency map and then get the standard deviation:

         Map<Integer, Integer> freq = new TreeMap<>();

         int iterations = 1000000;
         int bound = 100;
         int average = iterations / bound;

         start = System.currentTimeMillis();
         for (int i = 0; i < iterations; i++) {
             int x = random.nextInt(bound);
             freq.put(x, freq.getOrDefault(x, 0) + 1);
         }

         System.out.println("Time elapsed: " + (System.currentTimeMillis() - start));

         // calculate standard deviation
         double stddev = Math.sqrt(freq.entrySet().stream()
                 .mapToInt(Map.Entry::getValue)
                 .map(x -> (x - average) * (x - average))
                 .average()
                 .orElse(0.0));


         System.out.println("Std Dev for algorithm " + algo.name() + " : " + stddev);

Results:

Average Time elapsed for algorithm MERSENNE_TWISTER : 27.375
Distribution Test Time elapsed: 361
Std Dev for algorithm MERSENNE_TWISTER : 101.72708587195447

Average Time elapsed for algorithm XOR_SHIFT : 6.5
Distribution Test Time elapsed: 307
Std Dev for algorithm XOR_SHIFT : 100.46193308910594

Average Time elapsed for algorithm XORO_SHIFT : 6.5
Distribution Test Time elapsed: 386
Std Dev for algorithm XORO_SHIFT : 90.9447084771841

Average Time elapsed for algorithm SPLIT_MIX : 2.375
Distribution Test Time elapsed: 328
Std Dev for algorithm SPLIT_MIX : 97.59405719612235

r/javaexamples • • Jul 25 '17

[Java] Let's Code: Multithreaded Application Logger (11:56)

1 Upvotes

https://www.youtube.com/watch?v=rZ7T7zX4ZM4

This project is a java tutorial on how to make a logging mechanism for a multi-threaded application which may have more than one method of storing logging information.

The mechanism is implemented as a singleton which is a logging interface. This interface is agnostic and doesn't care about what method is actually being used to store the logging data. The program could be writing to a file, displaying in some GUI text area, writing to a database, or streaming over a network socket.

With this interface, the user doesn't need to continually change their code in order to implement a new logging method.

This is accomplished using Java's Observer/Observable mechanism so that one (or more) log message producers, can send log messages to one (or more) log message consumers. The method supports multiple log levels (debug, error, etc.).

P.S. Sorry about 480p! Next video will have better video quality

github: https://github.com/chrimatacode/app-log


r/javaexamples • • May 29 '17

How to test exceptions thrown with JUnit4 Rules

1 Upvotes
 @Rule
 public ExpectedException thrown = ExpectedException.none();

 @Test
 public void testReadFile() throws FileNotFoundException {

    thrown.expect(FileNotFoundException.class);
    thrown.expectMessage(startsWith("The file test.txt"));
    myClass.readFile("test.txt");
}