Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
@@ -0,0 +1,26 @@
package com.baeldung.assertion;
/**
* Simple demonstration of using Java assert keyword.
*/
public class Assertion {
public static void main(String[] args) {
Assertion assertion = new Assertion();
assertion.setup();
}
public void setup() {
Object conn = getConnection();
assert conn != null : "Connection is null";
// continue with other setup ...
}
// Simulate failure to get a connection; using Object
// to avoid dependencies on JDBC or some other heavy
// 3rd party library
public Object getConnection() {
return null;
}
}
@@ -0,0 +1,9 @@
package com.baeldung.className;
public class RetrievingClassName {
public class InnerClass {
}
}
@@ -0,0 +1,51 @@
package com.baeldung.comparable;
public class Player implements Comparable<Player> {
private int ranking;
private String name;
private int age;
public Player(int ranking, String name, int age) {
this.ranking = ranking;
this.name = name;
this.age = age;
}
public int getRanking() {
return ranking;
}
public void setRanking(int ranking) {
this.ranking = ranking;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return this.name;
}
@Override
public int compareTo(Player otherPlayer) {
return (this.getRanking() - otherPlayer.getRanking());
}
}
@@ -0,0 +1,25 @@
package com.baeldung.comparable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PlayerSorter {
public static void main(String[] args) {
List<Player> footballTeam = new ArrayList<Player>();
Player player1 = new Player(59, "John", 20);
Player player2 = new Player(67, "Roger", 22);
Player player3 = new Player(45, "Steven", 24);
footballTeam.add(player1);
footballTeam.add(player2);
footballTeam.add(player3);
System.out.println("Before Sorting : " + footballTeam);
Collections.sort(footballTeam);
System.out.println("After Sorting : " + footballTeam);
}
}
@@ -0,0 +1,46 @@
package com.baeldung.comparator;
public class Player {
private int ranking;
private String name;
private int age;
public Player(int ranking, String name, int age) {
this.ranking = ranking;
this.name = name;
this.age = age;
}
public int getRanking() {
return ranking;
}
public void setRanking(int ranking) {
this.ranking = ranking;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return this.name;
}
}
@@ -0,0 +1,12 @@
package com.baeldung.comparator;
import java.util.Comparator;
public class PlayerAgeComparator implements Comparator<Player> {
@Override
public int compare(Player firstPlayer, Player secondPlayer) {
return (firstPlayer.getAge() - secondPlayer.getAge());
}
}
@@ -0,0 +1,27 @@
package com.baeldung.comparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PlayerAgeSorter {
public static void main(String[] args) {
List<Player> footballTeam = new ArrayList<Player>();
Player player1 = new Player(59, "John", 22);
Player player2 = new Player(67, "Roger", 20);
Player player3 = new Player(45, "Steven", 24);
footballTeam.add(player1);
footballTeam.add(player2);
footballTeam.add(player3);
System.out.println("Before Sorting : " + footballTeam);
//Instance of PlayerAgeComparator
PlayerAgeComparator playerComparator = new PlayerAgeComparator();
Collections.sort(footballTeam, playerComparator);
System.out.println("After Sorting by age : " + footballTeam);
}
}
@@ -0,0 +1,12 @@
package com.baeldung.comparator;
import java.util.Comparator;
public class PlayerRankingComparator implements Comparator<Player> {
@Override
public int compare(Player firstPlayer, Player secondPlayer) {
return (firstPlayer.getRanking() - secondPlayer.getRanking());
}
}
@@ -0,0 +1,27 @@
package com.baeldung.comparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PlayerRankingSorter {
public static void main(String[] args) {
List<Player> footballTeam = new ArrayList<Player>();
Player player1 = new Player(59, "John", 22);
Player player2 = new Player(67, "Roger", 20);
Player player3 = new Player(45, "Steven", 40);
footballTeam.add(player1);
footballTeam.add(player2);
footballTeam.add(player3);
System.out.println("Before Sorting : " + footballTeam);
//Instance of PlayerRankingComparator
PlayerRankingComparator playerComparator = new PlayerRankingComparator();
Collections.sort(footballTeam, playerComparator);
System.out.println("After Sorting by ranking : " + footballTeam);
}
}
@@ -0,0 +1,17 @@
package com.baeldung.enums.values;
/**
* This is a simple enum of periodic table elements
*/
public enum Element1 {
H,
HE,
LI,
BE,
B,
C,
N,
O,
F,
NE
}
@@ -0,0 +1,52 @@
package com.baeldung.enums.values;
/**
* The simple enum has been enhanced to add the name of the element.
*/
public enum Element2 {
H("Hydrogen"),
HE("Helium"),
LI("Lithium"),
BE("Beryllium"),
B("Boron"),
C("Carbon"),
N("Nitrogen"),
O("Oxygen"),
F("Flourine"),
NE("Neon");
/** a final variable to store the label, which can't be changed */
public final String label;
/**
* A private constructor that sets the label.
* @param label
*/
private Element2(String label) {
this.label = label;
}
/**
* Look up Element2 instances by the label field. This implementation iterates through
* the values() list to find the label.
* @param label The label to look up
* @return The Element2 instance with the label, or null if not found.
*/
public static Element2 valueOfLabel(String label) {
for (Element2 e2 : values()) {
if (e2.label.equals(label)) {
return e2;
}
}
return null;
}
/**
* Override the toString() method to return the label instead of the declared name.
* @return
*/
@Override
public String toString() {
return this.label;
}
}
@@ -0,0 +1,63 @@
package com.baeldung.enums.values;
import java.util.HashMap;
import java.util.Map;
/**
* A Map has been added to cache labels for faster lookup.
*/
public enum Element3 {
H("Hydrogen"),
HE("Helium"),
LI("Lithium"),
BE("Beryllium"),
B("Boron"),
C("Carbon"),
N("Nitrogen"),
O("Oxygen"),
F("Flourine"),
NE("Neon");
/**
* A map to cache labels and their associated Element3 instances.
* Note that this only works if the labels are all unique!
*/
private static final Map<String, Element3> BY_LABEL = new HashMap<>();
/** populate the BY_LABEL cache */
static {
for (Element3 e3 : values()) {
BY_LABEL.put(e3.label, e3);
}
}
/** a final variable to store the label, which can't be changed */
public final String label;
/**
* A private constructor that sets the label.
* @param label
*/
private Element3(String label) {
this.label = label;
}
/**
* Look up Element2 instances by the label field. This implementation finds the
* label in the BY_LABEL cache.
* @param label The label to look up
* @return The Element3 instance with the label, or null if not found.
*/
public static Element3 valueOfLabel(String label) {
return BY_LABEL.get(label);
}
/**
* Override the toString() method to return the label instead of the declared name.
* @return
*/
@Override
public String toString() {
return this.label;
}
}
@@ -0,0 +1,95 @@
package com.baeldung.enums.values;
import java.util.HashMap;
import java.util.Map;
/**
* Multiple fields have been added and the Labeled interface is implemented.
*/
public enum Element4 implements Labeled {
H("Hydrogen", 1, 1.008f),
HE("Helium", 2, 4.0026f),
LI("Lithium", 3, 6.94f),
BE("Beryllium", 4, 9.01722f),
B("Boron", 5, 10.81f),
C("Carbon", 6, 12.011f),
N("Nitrogen", 7, 14.007f),
O("Oxygen", 8, 15.999f),
F("Flourine", 9, 18.998f),
NE("Neon", 10, 20.180f);
/**
* Maps cache labels and their associated Element3 instances.
* Note that this only works if the values are all unique!
*/
private static final Map<String, Element4> BY_LABEL = new HashMap<>();
private static final Map<Integer, Element4> BY_ATOMIC_NUMBER = new HashMap<>();
private static final Map<Float, Element4> BY_ATOMIC_WEIGHT = new HashMap<>();
/** populate the caches */
static {
for (Element4 e4 : values()) {
BY_LABEL.put(e4.label, e4);
BY_ATOMIC_NUMBER.put(e4.atomicNumber, e4);
BY_ATOMIC_WEIGHT.put(e4.atomicWeight, e4);
}
}
/** final variables to store the values, which can't be changed */
public final String label;
public final int atomicNumber;
public final float atomicWeight;
private Element4(String label, int atomicNumber, float atomicWeight) {
this.label = label;
this.atomicNumber = atomicNumber;
this.atomicWeight = atomicWeight;
}
/**
* Implement the Labeled interface.
* @return the label value
*/
@Override
public String label() {
return label;
}
/**
* Look up Element2 instances by the label field. This implementation finds the
* label in the BY_LABEL cache.
* @param label The label to look up
* @return The Element4 instance with the label, or null if not found.
*/
public static Element4 valueOfLabel(String label) {
return BY_LABEL.get(label);
}
/**
* Look up Element2 instances by the atomicNumber field. This implementation finds the
* atomicNUmber in the cache.
* @param number The atomicNumber to look up
* @return The Element4 instance with the label, or null if not found.
*/
public static Element4 valueOfAtomicNumber(int number) {
return BY_ATOMIC_NUMBER.get(number);
}
/**
* Look up Element2 instances by the atomicWeight field. This implementation finds the
* atomic weight in the cache.
* @param weight the atomic weight to look up
* @return The Element4 instance with the label, or null if not found.
*/
public static Element4 valueOfAtomicWeight(float weight) {
return BY_ATOMIC_WEIGHT.get(weight);
}
/**
* Override the toString() method to return the label instead of the declared name.
* @return
*/
@Override
public String toString() {
return this.label;
}
}
@@ -0,0 +1,5 @@
package com.baeldung.enums.values;
public interface Labeled {
String label();
}
@@ -0,0 +1,30 @@
package com.baeldung.finalize;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class CloseableResource implements AutoCloseable {
private BufferedReader reader;
public CloseableResource() {
InputStream input = this.getClass().getClassLoader().getResourceAsStream("file.txt");
reader = new BufferedReader(new InputStreamReader(input));
}
public String readFirstLine() throws IOException {
String firstLine = reader.readLine();
return firstLine;
}
@Override
public void close() {
try {
reader.close();
System.out.println("Closed BufferedReader in the close method");
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,30 @@
package com.baeldung.finalize;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Finalizable {
private BufferedReader reader;
public Finalizable() {
InputStream input = this.getClass().getClassLoader().getResourceAsStream("file.txt");
reader = new BufferedReader(new InputStreamReader(input));
}
public String readFirstLine() throws IOException {
String firstLine = reader.readLine();
return firstLine;
}
@Override
public void finalize() {
try {
reader.close();
System.out.println("Closed BufferedReader in the finalizer");
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,31 @@
package com.baeldung.recursion;
public class BinaryNode {
int value;
BinaryNode left;
BinaryNode right;
public BinaryNode(int value){
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public BinaryNode getLeft() {
return left;
}
public void setLeft(BinaryNode left) {
this.left = left;
}
public BinaryNode getRight() {
return right;
}
public void setRight(BinaryNode right) {
this.right = right;
}
}
@@ -0,0 +1,64 @@
package com.baeldung.recursion;
public class RecursionExample {
public int sum(int n){
if (n >= 1){
return sum(n - 1) + n;
}
return n;
}
public int tailSum(int currentSum, int n){
if (n <= 1) {
return currentSum + n;
}
return tailSum(currentSum + n, n - 1);
}
public int iterativeSum(int n){
int sum = 0;
if(n < 0){
return -1;
}
for(int i=0; i<=n; i++){
sum += i;
}
return sum;
}
public int powerOf10(int n){
if (n == 0){
return 1;
}
return powerOf10(n-1)*10;
}
public int fibonacci(int n){
if (n <=1 ){
return n;
}
return fibonacci(n-1) + fibonacci(n-2);
}
public String toBinary(int n){
if (n <= 1 ){
return String.valueOf(n);
}
return toBinary(n / 2) + String.valueOf(n % 2);
}
public int calculateTreeHeight(BinaryNode root){
if (root!= null){
if (root.getLeft() != null || root.getRight() != null){
return 1 + max(calculateTreeHeight(root.left) , calculateTreeHeight(root.right));
}
}
return 0;
}
public int max(int a,int b){
return a>b ? a:b;
}
}
@@ -0,0 +1,23 @@
package com.baeldung.synthetic;
import java.util.Comparator;
/**
* Class which contains a synthetic bridge method.
*
* @author Donato Rimenti
*
*/
public class BridgeMethodDemo implements Comparator<Integer> {
/*
* (non-Javadoc)
*
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
@Override
public int compare(Integer o1, Integer o2) {
return 0;
}
}
@@ -0,0 +1,34 @@
package com.baeldung.synthetic;
/**
* Wrapper for a class which contains a synthetic constructor.
*
* @author Donato Rimenti
*
*/
public class SyntheticConstructorDemo {
/**
* We need to instantiate the {@link NestedClass} using a private
* constructor from the enclosing instance in order to generate a synthetic
* constructor.
*/
private NestedClass nestedClass = new NestedClass();
/**
* Class which contains a synthetic constructor.
*
* @author Donato Rimenti
*
*/
class NestedClass {
/**
* In order to generate a synthetic constructor, this class must have a
* private constructor.
*/
private NestedClass() {
}
}
}
@@ -0,0 +1,22 @@
package com.baeldung.synthetic;
/**
* Wrapper for a class which contains a synthetic field reference to the outer
* class.
*
* @author Donato Rimenti
*
*/
public class SyntheticFieldDemo {
/**
* Class which contains a synthetic field reference to the outer class.
*
* @author Donato Rimenti
*
*/
class NestedClass {
}
}
@@ -0,0 +1,48 @@
package com.baeldung.synthetic;
/**
* Wrapper for a class which contains two synthetic methods accessors to a
* private field.
*
* @author Donato Rimenti
*
*/
public class SyntheticMethodDemo {
/**
* Class which contains two synthetic methods accessors to a private field.
*
* @author Donato Rimenti
*
*/
class NestedClass {
/**
* Field for which will be generated synthetic methods accessors. It's
* important that this field is private for this purpose.
*/
private String nestedField;
}
/**
* Gets the private nested field. We need to read the nested field in order
* to generate the synthetic getter.
*
* @return the {@link NestedClass#nestedField}
*/
public String getNestedField() {
return new NestedClass().nestedField;
}
/**
* Sets the private nested field. We need to write the nested field in order
* to generate the synthetic setter.
*
* @param nestedField
* the {@link NestedClass#nestedField}
*/
public void setNestedField(String nestedField) {
new NestedClass().nestedField = nestedField;
}
}
@@ -0,0 +1,17 @@
package com.baeldung.system;
import java.awt.event.WindowEvent;
/**
* Note: This class is not meant for unit-testing since it uses system
* features at low level and that it uses 'System' gc() which suggests
* JVM for garbage collection. But the usage below demonstrates how the
* method can be used.
*/
public class ChatWindow {
public void windowStateChanged(WindowEvent event) {
if (event.getNewState() == WindowEvent.WINDOW_DEACTIVATED ) {
System.gc(); // if it ends up running, great!
}
}
}
@@ -0,0 +1,16 @@
package com.baeldung.system;
import java.util.Date;
public class DateTimeService {
// One hour from now
public long nowPlusOneHour() {
return System.currentTimeMillis() + 3600 * 1000L;
}
// Human-readable format
public String nowPrettyPrinted() {
return new Date(System.currentTimeMillis()).toString();
}
}
@@ -0,0 +1,7 @@
package com.baeldung.system;
public class EnvironmentVariables {
public String getPath() {
return System.getenv("PATH");
}
}
@@ -0,0 +1,18 @@
package com.baeldung.system;
/**
* Note: This class is not meant for unit-testing since it uses system
* features at low level and that it uses 'System' standard error stream
* methods to show output on screen. Also unit-tests in CI environments
* don't have console output for user to see messages. But the usage below
* demonstrates how the methods can be used.
*/
public class SystemErrDemo {
public static void main(String[] args) {
// Print without 'hitting' return
System.err.print("some inline error message");
// Print and then 'hit' return
System.err.println("an error message having new line at the end");
}
}
@@ -0,0 +1,24 @@
package com.baeldung.system;
/**
* Note: This class is not meant for unit-testing since it uses system
* features at low level and that it uses 'System' exit() which will
* exit the JVM. Also unit-tests in CI environments are not meant to
* exit unit tests like that. But the usage below demonstrates how the
* method can be used.
*/
public class SystemExitDemo {
public static void main(String[] args) {
boolean error = false;
// do something and set error value
if (error) {
System.exit(1); // error case exit
} else {
System.exit(0); // normal case exit
}
// Will not do anything after exit()
}
}
@@ -0,0 +1,25 @@
package com.baeldung.system;
import java.io.FileNotFoundException;
import java.io.PrintStream;
/**
* Note: This class is not meant for unit-testing since it uses system
* features at low level and that it uses 'System' standard output stream
* methods to show output on screen. Also unit-tests in CI environments
* don't have console output for user to see messages. But the usage below
* demonstrates how the methods can be used.
*/
public class SystemOutDemo {
public static void main(String[] args) throws FileNotFoundException {
// Print without 'hitting' return
System.out.print("some inline message");
// Print and then 'hit' return
System.out.println("a message having new line at the end");
// Changes output stream to send messages to file.
System.setOut(new PrintStream("file.txt"));
}
}
@@ -0,0 +1,35 @@
package com.baeldung.system;
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Note: This class is not meant for unit-testing since it uses system
* features at low level and that it uses 'System' standard input stream
* methods to read text from user. Also unit-tests in CI environments
* don't have console input for user to type in text. But the usage below
* demonstrates how the methods can be used.
*/
public class UserCredentials {
public String readUsername(int length) throws IOException {
byte[] name = new byte[length];
System.in.read(name, 0, length); // by default, from the console
return new String(name);
}
public String readUsername() throws IOException {
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
return reader.readLine();
}
public String readUsernameWithPrompt() {
Console console = System.console();
return console == null ? null : // Console not available
console.readLine("%s", "Enter your name: ");
}
}
@@ -0,0 +1 @@
baeldung.com