Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Justin Albano 2019-09-13 21:14:15 -04:00
commit 9eb36c39c1
1592 changed files with 248419 additions and 8393 deletions

View File

@ -1,24 +0,0 @@
language: java
before_install:
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit'" > ~/.mavenrc
install: skip
script: travis_wait 60 mvn -q install -Pdefault-first,default-second -Dgib.enabled=true
sudo: required
jdk:
- oraclejdk8
addons:
apt:
packages:
- oracle-java8-installer
cache:
directories:
- .autoconf
- $HOME/.m2

View File

@ -7,3 +7,4 @@
- [Checking If a List Is Sorted in Java](https://www.baeldung.com/java-check-if-list-sorted)
- [Checking if a Java Graph has a Cycle](https://www.baeldung.com/java-graph-has-a-cycle)
- [A Guide to the Folding Technique in Java](https://www.baeldung.com/folding-hashing-technique)
- [Creating a Triangle with for Loops in Java](https://www.baeldung.com/java-print-triangle)

View File

@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>algorithms-miscellaneous-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
@ -18,17 +18,41 @@
<version>${org.assertj.core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>${retrofit.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-jackson</artifactId>
<version>${retrofit.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
@ -48,5 +72,6 @@
<org.assertj.core.version>3.9.0</org.assertj.core.version>
<commons-collections4.version>4.3</commons-collections4.version>
<guava.version>28.0-jre</guava.version>
<retrofit.version>2.6.0</retrofit.version>
</properties>
</project>

View File

@ -0,0 +1,70 @@
package com.baeldung.bucketsort;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class IntegerBucketSorter implements Sorter<Integer> {
private final Comparator<Integer> comparator;
public IntegerBucketSorter(Comparator<Integer> comparator) {
this.comparator = comparator;
}
public IntegerBucketSorter() {
comparator = Comparator.naturalOrder();
}
public List<Integer> sort(List<Integer> arrayToSort) {
List<List<Integer>> buckets = splitIntoUnsortedBuckets(arrayToSort);
for(List<Integer> bucket : buckets){
bucket.sort(comparator);
}
return concatenateSortedBuckets(buckets);
}
private List<Integer> concatenateSortedBuckets(List<List<Integer>> buckets){
List<Integer> sortedArray = new ArrayList<>();
int index = 0;
for(List<Integer> bucket : buckets){
for(int number : bucket){
sortedArray.add(index++, number);
}
}
return sortedArray;
}
private List<List<Integer>> splitIntoUnsortedBuckets(List<Integer> initialList){
final int[] codes = createHashes(initialList);
List<List<Integer>> buckets = new ArrayList<>(codes[1]);
for(int i = 0; i < codes[1]; i++) buckets.add(new ArrayList<>());
//distribute the data
for (int i : initialList) {
buckets.get(hash(i, codes)).add(i);
}
return buckets;
}
private int[] createHashes(List<Integer> input){
int m = input.get(0);
for (int i = 1; i < input.size(); i++) {
if (m < input.get(i)) {
m = input.get(i);
}
}
return new int[]{m, (int) Math.sqrt(input.size())};
}
private static int hash(int i, int[] code) {
return (int) ((double) i / code[0] * (code[1] - 1));
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.bucketsort;
import java.util.List;
public interface Sorter<T> {
List<T> sort(List<T> arrayToSort);
}

View File

@ -0,0 +1,35 @@
package com.baeldung.algorithms.interpolationsearch;
public class InterpolationSearch {
public static int interpolationSearch(int[] data, int item) {
int highEnd = (data.length - 1);
int lowEnd = 0;
while (item >= data[lowEnd] && item <= data[highEnd] && lowEnd <= highEnd) {
int probe = lowEnd + (highEnd - lowEnd) * (item - data[lowEnd]) / (data[highEnd] - data[lowEnd]);
if (highEnd == lowEnd) {
if (data[lowEnd] == item) {
return lowEnd;
} else {
return -1;
}
}
if (data[probe] == item) {
return probe;
}
if (data[probe] < item) {
lowEnd = probe + 1;
} else {
highEnd = probe - 1;
}
}
return -1;
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.algorithms.kmeans;
import java.util.Map;
import java.util.Objects;
/**
* Encapsulates all coordinates for a particular cluster centroid.
*/
public class Centroid {
/**
* The centroid coordinates.
*/
private final Map<String, Double> coordinates;
public Centroid(Map<String, Double> coordinates) {
this.coordinates = coordinates;
}
public Map<String, Double> getCoordinates() {
return coordinates;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Centroid centroid = (Centroid) o;
return Objects.equals(getCoordinates(), centroid.getCoordinates());
}
@Override
public int hashCode() {
return Objects.hash(getCoordinates());
}
@Override
public String toString() {
return "Centroid " + coordinates;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.algorithms.kmeans;
import java.util.Map;
/**
* Defines a contract to calculate distance between two feature vectors. The less the
* calculated distance, the more two items are similar to each other.
*/
public interface Distance {
/**
* Calculates the distance between two feature vectors.
*
* @param f1 The first set of features.
* @param f2 The second set of features.
* @return Calculated distance.
* @throws IllegalArgumentException If the given feature vectors are invalid.
*/
double calculate(Map<String, Double> f1, Map<String, Double> f2);
}

View File

@ -0,0 +1,23 @@
package com.baeldung.algorithms.kmeans;
import java.util.List;
import java.util.Map;
/**
* Encapsulates methods to calculates errors between centroid and the cluster members.
*/
public class Errors {
public static double sse(Map<Centroid, List<Record>> clustered, Distance distance) {
double sum = 0;
for (Map.Entry<Centroid, List<Record>> entry : clustered.entrySet()) {
Centroid centroid = entry.getKey();
for (Record record : entry.getValue()) {
double d = distance.calculate(centroid.getCoordinates(), record.getFeatures());
sum += Math.pow(d, 2);
}
}
return sum;
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.algorithms.kmeans;
import java.util.Map;
/**
* Calculates the distance between two items using the Euclidean formula.
*/
public class EuclideanDistance implements Distance {
@Override
public double calculate(Map<String, Double> f1, Map<String, Double> f2) {
if (f1 == null || f2 == null) {
throw new IllegalArgumentException("Feature vectors can't be null");
}
double sum = 0;
for (String key : f1.keySet()) {
Double v1 = f1.get(key);
Double v2 = f2.get(key);
if (v1 != null && v2 != null) sum += Math.pow(v1 - v2, 2);
}
return Math.sqrt(sum);
}
}

View File

@ -0,0 +1,236 @@
package com.baeldung.algorithms.kmeans;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
/**
* Encapsulates an implementation of KMeans clustering algorithm.
*
* @author Ali Dehghani
*/
public class KMeans {
private KMeans() {
throw new IllegalAccessError("You shouldn't call this constructor");
}
/**
* Will be used to generate random numbers.
*/
private static final Random random = new Random();
/**
* Performs the K-Means clustering algorithm on the given dataset.
*
* @param records The dataset.
* @param k Number of Clusters.
* @param distance To calculate the distance between two items.
* @param maxIterations Upper bound for the number of iterations.
* @return K clusters along with their features.
*/
public static Map<Centroid, List<Record>> fit(List<Record> records, int k, Distance distance, int maxIterations) {
applyPreconditions(records, k, distance, maxIterations);
List<Centroid> centroids = randomCentroids(records, k);
Map<Centroid, List<Record>> clusters = new HashMap<>();
Map<Centroid, List<Record>> lastState = new HashMap<>();
// iterate for a pre-defined number of times
for (int i = 0; i < maxIterations; i++) {
boolean isLastIteration = i == maxIterations - 1;
// in each iteration we should find the nearest centroid for each record
for (Record record : records) {
Centroid centroid = nearestCentroid(record, centroids, distance);
assignToCluster(clusters, record, centroid);
}
// if the assignment does not change, then the algorithm terminates
boolean shouldTerminate = isLastIteration || clusters.equals(lastState);
lastState = clusters;
if (shouldTerminate) {
break;
}
// at the end of each iteration we should relocate the centroids
centroids = relocateCentroids(clusters);
clusters = new HashMap<>();
}
return lastState;
}
/**
* Move all cluster centroids to the average of all assigned features.
*
* @param clusters The current cluster configuration.
* @return Collection of new and relocated centroids.
*/
private static List<Centroid> relocateCentroids(Map<Centroid, List<Record>> clusters) {
return clusters
.entrySet()
.stream()
.map(e -> average(e.getKey(), e.getValue()))
.collect(toList());
}
/**
* Moves the given centroid to the average position of all assigned features. If
* the centroid has no feature in its cluster, then there would be no need for a
* relocation. Otherwise, for each entry we calculate the average of all records
* first by summing all the entries and then dividing the final summation value by
* the number of records.
*
* @param centroid The centroid to move.
* @param records The assigned features.
* @return The moved centroid.
*/
private static Centroid average(Centroid centroid, List<Record> records) {
// if this cluster is empty, then we shouldn't move the centroid
if (records == null || records.isEmpty()) {
return centroid;
}
// Since some records don't have all possible attributes, we initialize
// average coordinates equal to current centroid coordinates
Map<String, Double> average = centroid.getCoordinates();
// The average function works correctly if we clear all coordinates corresponding
// to present record attributes
records
.stream()
.flatMap(e -> e
.getFeatures()
.keySet()
.stream())
.forEach(k -> average.put(k, 0.0));
for (Record record : records) {
record
.getFeatures()
.forEach((k, v) -> average.compute(k, (k1, currentValue) -> v + currentValue));
}
average.forEach((k, v) -> average.put(k, v / records.size()));
return new Centroid(average);
}
/**
* Assigns a feature vector to the given centroid. If this is the first assignment for this centroid,
* first we should create the list.
*
* @param clusters The current cluster configuration.
* @param record The feature vector.
* @param centroid The centroid.
*/
private static void assignToCluster(Map<Centroid, List<Record>> clusters, Record record, Centroid centroid) {
clusters.compute(centroid, (key, list) -> {
if (list == null) {
list = new ArrayList<>();
}
list.add(record);
return list;
});
}
/**
* With the help of the given distance calculator, iterates through centroids and finds the
* nearest one to the given record.
*
* @param record The feature vector to find a centroid for.
* @param centroids Collection of all centroids.
* @param distance To calculate the distance between two items.
* @return The nearest centroid to the given feature vector.
*/
private static Centroid nearestCentroid(Record record, List<Centroid> centroids, Distance distance) {
double minimumDistance = Double.MAX_VALUE;
Centroid nearest = null;
for (Centroid centroid : centroids) {
double currentDistance = distance.calculate(record.getFeatures(), centroid.getCoordinates());
if (currentDistance < minimumDistance) {
minimumDistance = currentDistance;
nearest = centroid;
}
}
return nearest;
}
/**
* Generates k random centroids. Before kicking-off the centroid generation process,
* first we calculate the possible value range for each attribute. Then when
* we're going to generate the centroids, we generate random coordinates in
* the [min, max] range for each attribute.
*
* @param records The dataset which helps to calculate the [min, max] range for
* each attribute.
* @param k Number of clusters.
* @return Collections of randomly generated centroids.
*/
private static List<Centroid> randomCentroids(List<Record> records, int k) {
List<Centroid> centroids = new ArrayList<>();
Map<String, Double> maxs = new HashMap<>();
Map<String, Double> mins = new HashMap<>();
for (Record record : records) {
record
.getFeatures()
.forEach((key, value) -> {
// compares the value with the current max and choose the bigger value between them
maxs.compute(key, (k1, max) -> max == null || value > max ? value : max);
// compare the value with the current min and choose the smaller value between them
mins.compute(key, (k1, min) -> min == null || value < min ? value : min);
});
}
Set<String> attributes = records
.stream()
.flatMap(e -> e
.getFeatures()
.keySet()
.stream())
.collect(toSet());
for (int i = 0; i < k; i++) {
Map<String, Double> coordinates = new HashMap<>();
for (String attribute : attributes) {
double max = maxs.get(attribute);
double min = mins.get(attribute);
coordinates.put(attribute, random.nextDouble() * (max - min) + min);
}
centroids.add(new Centroid(coordinates));
}
return centroids;
}
private static void applyPreconditions(List<Record> records, int k, Distance distance, int maxIterations) {
if (records == null || records.isEmpty()) {
throw new IllegalArgumentException("The dataset can't be empty");
}
if (k <= 1) {
throw new IllegalArgumentException("It doesn't make sense to have less than or equal to 1 cluster");
}
if (distance == null) {
throw new IllegalArgumentException("The distance calculator is required");
}
if (maxIterations <= 0) {
throw new IllegalArgumentException("Max iterations should be a positive number");
}
}
}

View File

@ -0,0 +1,144 @@
package com.baeldung.algorithms.kmeans;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
import static java.util.stream.Collectors.toSet;
public class LastFm {
private static OkHttpClient okHttp = new OkHttpClient.Builder()
.addInterceptor(new LastFmService.Authenticator("put your API key here"))
.build();
private static Retrofit retrofit = new Retrofit.Builder()
.client(okHttp)
.addConverterFactory(JacksonConverterFactory.create())
.baseUrl("http://ws.audioscrobbler.com/")
.build();
private static LastFmService lastFm = retrofit.create(LastFmService.class);
private static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws IOException {
List<String> artists = getTop100Artists();
Set<String> tags = getTop100Tags();
List<Record> records = datasetWithTaggedArtists(artists, tags);
Map<Centroid, List<Record>> clusters = KMeans.fit(records, 7, new EuclideanDistance(), 1000);
// Print the cluster configuration
clusters.forEach((key, value) -> {
System.out.println("------------------------------ CLUSTER -----------------------------------");
System.out.println(sortedCentroid(key));
String members = String.join(", ", value
.stream()
.map(Record::getDescription)
.collect(toSet()));
System.out.print(members);
System.out.println();
System.out.println();
});
Map<String, Object> json = convertToD3CompatibleMap(clusters);
System.out.println(mapper.writeValueAsString(json));
}
private static Map<String, Object> convertToD3CompatibleMap(Map<Centroid, List<Record>> clusters) {
Map<String, Object> json = new HashMap<>();
json.put("name", "Musicians");
List<Map<String, Object>> children = new ArrayList<>();
clusters.forEach((key, value) -> {
Map<String, Object> child = new HashMap<>();
child.put("name", dominantGenre(sortedCentroid(key)));
List<Map<String, String>> nested = new ArrayList<>();
for (Record record : value) {
nested.add(Collections.singletonMap("name", record.getDescription()));
}
child.put("children", nested);
children.add(child);
});
json.put("children", children);
return json;
}
private static String dominantGenre(Centroid centroid) {
return centroid
.getCoordinates()
.keySet()
.stream()
.limit(2)
.collect(Collectors.joining(", "));
}
private static Centroid sortedCentroid(Centroid key) {
List<Map.Entry<String, Double>> entries = new ArrayList<>(key
.getCoordinates()
.entrySet());
entries.sort((e1, e2) -> e2
.getValue()
.compareTo(e1.getValue()));
Map<String, Double> sorted = new LinkedHashMap<>();
for (Map.Entry<String, Double> entry : entries) {
sorted.put(entry.getKey(), entry.getValue());
}
return new Centroid(sorted);
}
private static List<Record> datasetWithTaggedArtists(List<String> artists, Set<String> topTags) throws IOException {
List<Record> records = new ArrayList<>();
for (String artist : artists) {
Map<String, Double> tags = lastFm
.topTagsFor(artist)
.execute()
.body()
.all();
// Only keep popular tags.
tags
.entrySet()
.removeIf(e -> !topTags.contains(e.getKey()));
records.add(new Record(artist, tags));
}
return records;
}
private static Set<String> getTop100Tags() throws IOException {
return lastFm
.topTags()
.execute()
.body()
.all();
}
private static List<String> getTop100Artists() throws IOException {
List<String> artists = new ArrayList<>();
for (int i = 1; i <= 2; i++) {
artists.addAll(lastFm
.topArtists(i)
.execute()
.body()
.all());
}
return artists;
}
}

View File

@ -0,0 +1,118 @@
package com.baeldung.algorithms.kmeans;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
import static java.util.stream.Collectors.toList;
public interface LastFmService {
@GET("/2.0/?method=chart.gettopartists&format=json&limit=50")
Call<Artists> topArtists(@Query("page") int page);
@GET("/2.0/?method=artist.gettoptags&format=json&limit=20&autocorrect=1")
Call<Tags> topTagsFor(@Query("artist") String artist);
@GET("/2.0/?method=chart.gettoptags&format=json&limit=100")
Call<TopTags> topTags();
/**
* HTTP interceptor to intercept all HTTP requests and add the API key to them.
*/
class Authenticator implements Interceptor {
private final String apiKey;
Authenticator(String apiKey) {
this.apiKey = apiKey;
}
@Override
public Response intercept(Chain chain) throws IOException {
HttpUrl url = chain
.request()
.url()
.newBuilder()
.addQueryParameter("api_key", apiKey)
.build();
Request request = chain
.request()
.newBuilder()
.url(url)
.build();
return chain.proceed(request);
}
}
@JsonAutoDetect(fieldVisibility = ANY)
class TopTags {
private Map<String, Object> tags;
@SuppressWarnings("unchecked")
public Set<String> all() {
List<Map<String, Object>> topTags = (List<Map<String, Object>>) tags.get("tag");
return topTags
.stream()
.map(e -> ((String) e.get("name")))
.collect(Collectors.toSet());
}
}
@JsonAutoDetect(fieldVisibility = ANY)
class Tags {
@JsonProperty("toptags") private Map<String, Object> topTags;
@SuppressWarnings("unchecked")
public Map<String, Double> all() {
try {
Map<String, Double> all = new HashMap<>();
List<Map<String, Object>> tags = (List<Map<String, Object>>) topTags.get("tag");
for (Map<String, Object> tag : tags) {
all.put(((String) tag.get("name")), ((Integer) tag.get("count")).doubleValue());
}
return all;
} catch (Exception e) {
return Collections.emptyMap();
}
}
}
@JsonAutoDetect(fieldVisibility = ANY)
class Artists {
private Map<String, Object> artists;
@SuppressWarnings("unchecked")
public List<String> all() {
try {
List<Map<String, Object>> artists = (List<Map<String, Object>>) this.artists.get("artist");
return artists
.stream()
.map(e -> ((String) e.get("name")))
.collect(toList());
} catch (Exception e) {
return Collections.emptyList();
}
}
}
}

View File

@ -0,0 +1,65 @@
package com.baeldung.algorithms.kmeans;
import java.util.Map;
import java.util.Objects;
/**
* Encapsulates all feature values for a few attributes. Optionally each record
* can be described with the {@link #description} field.
*/
public class Record {
/**
* The record description. For example, this can be the artist name for the famous musician
* example.
*/
private final String description;
/**
* Encapsulates all attributes and their corresponding values, i.e. features.
*/
private final Map<String, Double> features;
public Record(String description, Map<String, Double> features) {
this.description = description;
this.features = features;
}
public Record(Map<String, Double> features) {
this("", features);
}
public String getDescription() {
return description;
}
public Map<String, Double> getFeatures() {
return features;
}
@Override
public String toString() {
String prefix = description == null || description
.trim()
.isEmpty() ? "Record" : description;
return prefix + ": " + features;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Record record = (Record) o;
return Objects.equals(getDescription(), record.getDescription()) && Objects.equals(getFeatures(), record.getFeatures());
}
@Override
public int hashCode() {
return Objects.hash(getDescription(), getFeatures());
}
}

View File

@ -0,0 +1,61 @@
package com.baeldung.algorithms.printtriangles;
import org.apache.commons.lang3.StringUtils;
public class PrintTriangleExamples {
public static String printARightTriangle(int N) {
StringBuilder result = new StringBuilder();
for (int r = 1; r <= N; r++) {
for (int j = 1; j <= r; j++) {
result.append("*");
}
result.append(System.lineSeparator());
}
return result.toString();
}
public static String printAnIsoscelesTriangle(int N) {
StringBuilder result = new StringBuilder();
for (int r = 1; r <= N; r++) {
for (int sp = 1; sp <= N - r; sp++) {
result.append(" ");
}
for (int c = 1; c <= (r * 2) - 1; c++) {
result.append("*");
}
result.append(System.lineSeparator());
}
return result.toString();
}
public static String printAnIsoscelesTriangleUsingStringUtils(int N) {
StringBuilder result = new StringBuilder();
for (int r = 1; r <= N; r++) {
result.append(StringUtils.repeat(' ', N - r));
result.append(StringUtils.repeat('*', 2 * r - 1));
result.append(System.lineSeparator());
}
return result.toString();
}
public static String printAnIsoscelesTriangleUsingSubstring(int N) {
StringBuilder result = new StringBuilder();
String helperString = StringUtils.repeat(' ', N - 1) + StringUtils.repeat('*', N * 2 - 1);
for (int r = 0; r < N; r++) {
result.append(helperString.substring(r, N + 2 * r));
result.append(System.lineSeparator());
}
return result.toString();
}
public static void main(String[] args) {
System.out.println(printARightTriangle(5));
System.out.println(printAnIsoscelesTriangle(5));
System.out.println(printAnIsoscelesTriangleUsingStringUtils(5));
System.out.println(printAnIsoscelesTriangleUsingSubstring(5));
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,490 @@
{
"children": [
{
"children": [
{
"name": "Radiohead"
},
{
"name": "Red Hot Chili Peppers"
},
{
"name": "Coldplay"
},
{
"name": "Nirvana"
},
{
"name": "Panic! at the Disco"
},
{
"name": "The Cure"
},
{
"name": "Linkin Park"
},
{
"name": "Radiohead"
},
{
"name": "Red Hot Chili Peppers"
},
{
"name": "Coldplay"
},
{
"name": "Nirvana"
},
{
"name": "Panic! at the Disco"
},
{
"name": "The Cure"
},
{
"name": "Linkin Park"
},
{
"name": "Muse"
},
{
"name": "Maroon 5"
},
{
"name": "Foo Fighters"
},
{
"name": "Paramore"
},
{
"name": "Oasis"
},
{
"name": "Fall Out Boy"
},
{
"name": "OneRepublic"
},
{
"name": "Weezer"
},
{
"name": "System of a Down"
},
{
"name": "The White Stripes"
}
],
"name": "rock, alternative"
},
{
"children": [
{
"name": "Lil Nas X"
},
{
"name": "Post Malone"
},
{
"name": "Drake"
},
{
"name": "Kanye West"
},
{
"name": "Kendrick Lamar"
},
{
"name": "Tyler, the Creator"
},
{
"name": "Eminem"
},
{
"name": "Childish Gambino"
},
{
"name": "Frank Ocean"
},
{
"name": "Lil Nas X"
},
{
"name": "Post Malone"
},
{
"name": "Drake"
},
{
"name": "Kanye West"
},
{
"name": "Kendrick Lamar"
},
{
"name": "Tyler, the Creator"
},
{
"name": "Eminem"
},
{
"name": "Childish Gambino"
},
{
"name": "Frank Ocean"
},
{
"name": "Lizzo"
},
{
"name": "Travi$ Scott"
},
{
"name": "A$AP Rocky"
},
{
"name": "Nicki Minaj"
},
{
"name": "xxxtentacion"
}
],
"name": "Hip-Hop, rap"
},
{
"children": [
{
"name": "Arctic Monkeys"
},
{
"name": "Imagine Dragons"
},
{
"name": "The Killers"
},
{
"name": "Gorillaz"
},
{
"name": "The Black Keys"
},
{
"name": "Arctic Monkeys"
},
{
"name": "Imagine Dragons"
},
{
"name": "The Killers"
},
{
"name": "Gorillaz"
},
{
"name": "The Black Keys"
},
{
"name": "Twenty One Pilots"
},
{
"name": "Ellie Goulding"
},
{
"name": "Florence + the Machine"
},
{
"name": "Vampire Weekend"
},
{
"name": "The Smiths"
},
{
"name": "The Strokes"
},
{
"name": "MGMT"
},
{
"name": "Foster the People"
},
{
"name": "Two Door Cinema Club"
},
{
"name": "Cage the Elephant"
},
{
"name": "Arcade Fire"
},
{
"name": "The 1975"
}
],
"name": "indie, alternative"
},
{
"children": [
{
"name": "Ed Sheeran"
},
{
"name": "Tame Impala"
},
{
"name": "Ed Sheeran"
},
{
"name": "Tame Impala"
},
{
"name": "Green Day"
},
{
"name": "Metallica"
},
{
"name": "blink-182"
},
{
"name": "Bon Iver"
},
{
"name": "The Clash"
}
],
"name": "rock, punk rock"
},
{
"children": [
{
"name": "Calvin Harris"
},
{
"name": "The Weeknd"
},
{
"name": "The Chainsmokers"
},
{
"name": "Daft Punk"
},
{
"name": "Marshmello"
},
{
"name": "David Guetta"
},
{
"name": "Calvin Harris"
},
{
"name": "The Weeknd"
},
{
"name": "The Chainsmokers"
},
{
"name": "Daft Punk"
},
{
"name": "Marshmello"
},
{
"name": "David Guetta"
},
{
"name": "Avicii"
},
{
"name": "Kygo"
},
{
"name": "Martin Garrix"
},
{
"name": "Major Lazer"
},
{
"name": "Depeche Mode"
}
],
"name": "electronic, dance"
},
{
"children": [
{
"name": "Queen"
},
{
"name": "The Beatles"
},
{
"name": "David Bowie"
},
{
"name": "Fleetwood Mac"
},
{
"name": "Pink Floyd"
},
{
"name": "The Rolling Stones"
},
{
"name": "Led Zeppelin"
},
{
"name": "Queen"
},
{
"name": "The Beatles"
},
{
"name": "David Bowie"
},
{
"name": "Fleetwood Mac"
},
{
"name": "Pink Floyd"
},
{
"name": "The Rolling Stones"
},
{
"name": "Led Zeppelin"
},
{
"name": "Elton John"
}
],
"name": "classic rock, rock"
},
{
"children": [
{
"name": "Billie Eilish"
},
{
"name": "Ariana Grande"
},
{
"name": "Taylor Swift"
},
{
"name": "Beyoncé"
},
{
"name": "Shawn Mendes"
},
{
"name": "Rihanna"
},
{
"name": "Lana Del Rey"
},
{
"name": "Katy Perry"
},
{
"name": "Lady Gaga"
},
{
"name": "Miley Cyrus"
},
{
"name": "Mark Ronson"
},
{
"name": "Madonna"
},
{
"name": "Lorde"
},
{
"name": "Khalid"
},
{
"name": "Billie Eilish"
},
{
"name": "Ariana Grande"
},
{
"name": "Taylor Swift"
},
{
"name": "Beyoncé"
},
{
"name": "Shawn Mendes"
},
{
"name": "Rihanna"
},
{
"name": "Lana Del Rey"
},
{
"name": "Katy Perry"
},
{
"name": "Lady Gaga"
},
{
"name": "Miley Cyrus"
},
{
"name": "Mark Ronson"
},
{
"name": "Madonna"
},
{
"name": "Lorde"
},
{
"name": "Khalid"
},
{
"name": "Sia"
},
{
"name": "Sam Smith"
},
{
"name": "Halsey"
},
{
"name": "Michael Jackson"
},
{
"name": "Charli XCX"
},
{
"name": "Britney Spears"
},
{
"name": "Dua Lipa"
},
{
"name": "Jonas Brothers"
},
{
"name": "Bruno Mars"
},
{
"name": "Carly Rae Jepsen"
},
{
"name": "P!nk"
},
{
"name": "Adele"
}
],
"name": "pop, female vocalists"
}
],
"name": "Musicians"
}

View File

@ -0,0 +1,68 @@
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 1.5px;
}
.node {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var diameter = 1100;
var tree = d3.layout.tree()
.size([360, diameter / 2 - 300])
.separation(function (a, b) {
return (a.parent == b.parent ? 1 : 2) / a.depth;
});
var diagonal = d3.svg.diagonal.radial()
.projection(function (d) {
return [d.y, d.x / 180 * Math.PI];
});
var svg = d3.select("body").append("svg")
.attr("width", diameter)
.attr("height", diameter - 150)
.append("g")
.attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");
d3.json("lastfm.json", function (error, root) {
var nodes = tree.nodes(root),
links = tree.links(nodes);
var link = svg.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var node = svg.selectAll(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function (d) {
return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")";
})
node.append("circle")
.attr("r", 4.5);
node.append("text")
.attr("dy", ".31em")
.attr("text-anchor", function (d) {
return d.x < 180 ? "start" : "end";
})
.attr("transform", function (d) {
return d.x < 180 ? "translate(8)" : "rotate(180)translate(-8)";
})
.text(function (d) {
return d.name;
});
});
d3.select(self.frameElement).style("height", diameter - 150 + "px");
</script>

View File

@ -0,0 +1,33 @@
package com.baeldung.bucketsort;
import com.baeldung.bucketsort.IntegerBucketSorter;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class IntegerBucketSorterUnitTest {
private IntegerBucketSorter sorter;
@Before
public void setUp() throws Exception {
sorter = new IntegerBucketSorter();
}
@Test
public void givenUnsortedList_whenSortedUsingBucketSorter_checkSortingCorrect() {
List<Integer> unsorted = Arrays.asList(80,50,60,30,20,10,70,0,40,500,600,602,200,15);
List<Integer> expected = Arrays.asList(0,10,15,20,30,40,50,60,70,80,200,500,600,602);
List<Integer> actual = sorter.sort(unsorted);
assertEquals(expected, actual);
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.algorithms.interpolationsearch;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class InterpolationSearchUnitTest {
private int[] myData;
@Before
public void setUp() {
myData = new int[]{13,21,34,55,69,73,84,101};
}
@Test
public void givenSortedArray_whenLookingFor84_thenReturn6() {
int pos = InterpolationSearch.interpolationSearch(myData, 84);
assertEquals(6, pos);
}
@Test
public void givenSortedArray_whenLookingFor19_thenReturnMinusOne() {
int pos = InterpolationSearch.interpolationSearch(myData, 19);
assertEquals(-1, pos);
}
}

View File

@ -0,0 +1,101 @@
package com.baeldung.algorithms.printtriangles;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
@RunWith(JUnitParamsRunner.class)
public class PrintTriangleExamplesUnitTest {
private static Object[][] rightTriangles() {
String expected0 = "";
String expected2 = "*" + System.lineSeparator()
+ "**" + System.lineSeparator();
String expected5 = "*" + System.lineSeparator()
+ "**" + System.lineSeparator()
+ "***" + System.lineSeparator()
+ "****" + System.lineSeparator()
+ "*****" + System.lineSeparator();
String expected7 = "*" + System.lineSeparator()
+ "**" + System.lineSeparator()
+ "***" + System.lineSeparator()
+ "****" + System.lineSeparator()
+ "*****" + System.lineSeparator()
+ "******" + System.lineSeparator()
+ "*******" + System.lineSeparator();
return new Object[][] {
{ 0, expected0 },
{ 2, expected2 },
{ 5, expected5 },
{ 7, expected7 }
};
}
@Test
@Parameters(method = "rightTriangles")
public void whenPrintARightTriangleIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) {
String actual = PrintTriangleExamples.printARightTriangle(nrOfRows);
assertEquals(expected, actual);
}
private static Object[][] isoscelesTriangles() {
String expected0 = "";
String expected2 = " *" + System.lineSeparator()
+ "***" + System.lineSeparator();
String expected5 = " *" + System.lineSeparator()
+ " ***" + System.lineSeparator()
+ " *****" + System.lineSeparator()
+ " *******" + System.lineSeparator()
+ "*********" + System.lineSeparator();
String expected7 = " *" + System.lineSeparator()
+ " ***" + System.lineSeparator()
+ " *****" + System.lineSeparator()
+ " *******" + System.lineSeparator()
+ " *********" + System.lineSeparator()
+ " ***********" + System.lineSeparator()
+ "*************" + System.lineSeparator();
return new Object[][] {
{ 0, expected0 },
{ 2, expected2 },
{ 5, expected5 },
{ 7, expected7 }
};
}
@Test
@Parameters(method = "isoscelesTriangles")
public void whenPrintAnIsoscelesTriangleIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) {
String actual = PrintTriangleExamples.printAnIsoscelesTriangle(nrOfRows);
assertEquals(expected, actual);
}
@Test
@Parameters(method = "isoscelesTriangles")
public void whenPrintAnIsoscelesTriangleUsingStringUtilsIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) {
String actual = PrintTriangleExamples.printAnIsoscelesTriangleUsingStringUtils(nrOfRows);
assertEquals(expected, actual);
}
@Test
@Parameters(method = "isoscelesTriangles")
public void whenPrintAnIsoscelesTriangleUsingSubstringIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) {
String actual = PrintTriangleExamples.printAnIsoscelesTriangleUsingSubstring(nrOfRows);
assertEquals(expected, actual);
}
}

View File

@ -5,3 +5,5 @@
- [Quicksort Algorithm Implementation in Java](https://www.baeldung.com/java-quicksort)
- [Insertion Sort in Java](https://www.baeldung.com/java-insertion-sort)
- [Heap Sort in Java](https://www.baeldung.com/java-heap-sort)
- [Shell Sort in Java](https://www.baeldung.com/java-shell-sort)
- [Counting Sort in Java](https://www.baeldung.com/java-counting-sort)

View File

@ -0,0 +1,48 @@
package com.baeldung.algorithms.counting;
import java.util.Arrays;
import java.util.stream.IntStream;
public class CountingSort {
public static int[] sort(int[] input, int k) {
verifyPreconditions(input, k);
if (input.length == 0) return input;
int[] c = countElements(input, k);
int[] sorted = new int[input.length];
for (int i = input.length - 1; i >= 0; i--) {
int current = input[i];
sorted[c[current] - 1] = current;
c[current] -= 1;
}
return sorted;
}
static int[] countElements(int[] input, int k) {
int[] c = new int[k + 1];
Arrays.fill(c, 0);
for (int i : input) {
c[i] += 1;
}
for (int i = 1; i < c.length; i++) {
c[i] += c[i - 1];
}
return c;
}
private static void verifyPreconditions(int[] input, int k) {
if (input == null) {
throw new IllegalArgumentException("Input is required");
}
int min = IntStream.of(input).min().getAsInt();
int max = IntStream.of(input).max().getAsInt();
if (min < 0 || max > k) {
throw new IllegalArgumentException("The input numbers should be between zero and " + k);
}
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.algorithms.inoutsort;
public class InOutSort {
public static int[] reverseInPlace(int A[]) {
int n = A.length;
for (int i = 0; i < n / 2; i++) {
int temp = A[i];
A[i] = A[n - 1 - i];
A[n - 1 - i] = temp;
}
return A;
}
public static int[] reverseOutOfPlace(int A[]) {
int n = A.length;
int[] B = new int[n];
for (int i = 0; i < n; i++) {
B[n - i - 1] = A[i];
}
return B;
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.algorithms.radixsort;
import java.util.Arrays;
public class RadixSort {
public static void sort(int numbers[]) {
int maximumNumber = findMaximumNumberIn(numbers);
int numberOfDigits = calculateNumberOfDigitsIn(maximumNumber);
int placeValue = 1;
while (numberOfDigits-- > 0) {
applyCountingSortOn(numbers, placeValue);
placeValue *= 10;
}
}
private static void applyCountingSortOn(int[] numbers, int placeValue) {
int range = 10; // radix or the base
int length = numbers.length;
int[] frequency = new int[range];
int[] sortedValues = new int[length];
for (int i = 0; i < length; i++) {
int digit = (numbers[i] / placeValue) % range;
frequency[digit]++;
}
for (int i = 1; i < range; i++) {
frequency[i] += frequency[i - 1];
}
for (int i = length - 1; i >= 0; i--) {
int digit = (numbers[i] / placeValue) % range;
sortedValues[frequency[digit] - 1] = numbers[i];
frequency[digit]--;
}
System.arraycopy(sortedValues, 0, numbers, 0, length);
}
private static int calculateNumberOfDigitsIn(int number) {
return (int) Math.log10(number) + 1; // valid only if number > 0
}
private static int findMaximumNumberIn(int[] arr) {
return Arrays.stream(arr).max().getAsInt();
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.algorithms.stringsortingbynumber;
package com.baeldung.algorithms.sort.bynumber;
import java.util.Comparator;

View File

@ -0,0 +1,32 @@
package com.baeldung.algorithms.counting;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
class CountingSortUnitTest {
@Test
void countElements_GivenAnArray_ShouldCalculateTheFrequencyArrayAsExpected() {
int k = 5;
int[] input = { 4, 3, 2, 5, 4, 3, 5, 1, 0, 2, 5 };
int[] c = CountingSort.countElements(input, k);
int[] expected = { 1, 2, 4, 6, 8, 11 };
assertArrayEquals(expected, c);
}
@Test
void sort_GivenAnArray_ShouldSortTheInputAsExpected() {
int k = 5;
int[] input = { 4, 3, 2, 5, 4, 3, 5, 1, 0, 2, 5 };
int[] sorted = CountingSort.sort(input, k);
// Our sorting algorithm and Java's should return the same result
Arrays.sort(input);
assertArrayEquals(input, sorted);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.algorithms.inoutsort;
import static org.junit.Assert.*;
import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;
public class InOutSortUnitTest {
@Test
public void givenArray_whenInPlaceSort_thenReversed() {
int[] input = {1, 2, 3, 4, 5, 6, 7};
int[] expected = {7, 6, 5, 4, 3, 2, 1};
assertArrayEquals("the two arrays are not equal", expected, InOutSort.reverseInPlace(input));
}
@Test
public void givenArray_whenOutOfPlaceSort_thenReversed() {
int[] input = {1, 2, 3, 4, 5, 6, 7};
int[] expected = {7, 6, 5, 4, 3, 2, 1};
assertArrayEquals("the two arrays are not equal", expected, InOutSort.reverseOutOfPlace(input));
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.algorithms.radixsort;
import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;
public class RadixSortUnitTest {
@Test
public void givenUnsortedArray_whenRadixSort_thenArraySorted() {
int[] numbers = { 387, 468, 134, 123, 68, 221, 769, 37, 7 };
RadixSort.sort(numbers);
int[] numbersSorted = { 7, 37, 68, 123, 134, 221, 387, 468, 769 };
assertArrayEquals(numbersSorted, numbers);
}
}

View File

@ -1,6 +1,6 @@
package com.baeldung.algorithms.stringsortingbynumber;
package com.baeldung.algorithms.sort.bynumber;
import com.baeldung.algorithms.stringsortingbynumber.NaturalOrderComparators;
import com.baeldung.algorithms.sort.bynumber.NaturalOrderComparators;
import org.junit.Test;
import java.util.ArrayList;

View File

@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung.examples.olingo2</groupId>
<artifactId>olingo2-sample</artifactId>
<artifactId>olingo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>olingo2-sample</name>
<name>olingo2</name>
<description>Sample Olingo 2 Project</description>
<parent>

View File

@ -38,17 +38,6 @@
<artifactId>jcl-over-slf4j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j-version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<properties>
@ -56,4 +45,4 @@
<log4j-version>1.2.17</log4j-version>
</properties>
</project>
</project>

View File

@ -18,22 +18,17 @@ import javax.servlet.http.HttpServletRequest;
@Controller
public class ShiroSpringController {
@GetMapping("/")
public String index() {
return "index";
}
@RequestMapping( value = "/login", method = {RequestMethod.GET, RequestMethod.POST})
public String login(HttpServletRequest req, UserCredentials cred, RedirectAttributes attr) {
if(req.getMethod().equals(RequestMethod.GET.toString())) {
return "login";
}
else {
} else {
Subject subject = SecurityUtils.getSubject();
if(!subject.isAuthenticated()) {

View File

@ -0,0 +1,68 @@
package com.baeldung.shiro.permissions.custom;
import com.baeldung.MyCustomRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.Ini;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
private static final transient Logger log = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
IniRealm realm = new IniRealm();
Ini ini = Ini.fromResourcePath(Main.class.getResource("/com/baeldung/shiro/permissions/custom/shiro.ini").getPath());
realm.setIni(ini);
realm.setPermissionResolver(new PathPermissionResolver());
realm.init();
SecurityManager securityManager = new DefaultSecurityManager(realm);
SecurityUtils.setSecurityManager(securityManager);
Subject currentUser = SecurityUtils.getSubject();
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken("paul.reader", "password4");
token.setRememberMe(true);
try {
currentUser.login(token);
} catch (UnknownAccountException uae) {
log.error("Username Not Found!", uae);
} catch (IncorrectCredentialsException ice) {
log.error("Invalid Credentials!", ice);
} catch (LockedAccountException lae) {
log.error("Your Account is Locked!", lae);
} catch (AuthenticationException ae) {
log.error("Unexpected Error!", ae);
}
}
log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
if (currentUser.hasRole("admin")) {
log.info("Welcome Admin");
} else if(currentUser.hasRole("editor")) {
log.info("Welcome, Editor!");
} else if(currentUser.hasRole("author")) {
log.info("Welcome, Author");
} else {
log.info("Welcome, Guest");
}
if(currentUser.isPermitted("/articles/drafts/new-article")) {
log.info("You can access articles");
} else {
log.info("You cannot access articles!");
}
currentUser.logout();
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.shiro.permissions.custom;
import org.apache.shiro.authz.Permission;
import java.nio.file.Path;
public class PathPermission implements Permission {
private final Path path;
public PathPermission(Path path) {
this.path = path;
}
@Override
public boolean implies(Permission p) {
if(p instanceof PathPermission) {
return ((PathPermission) p).path.startsWith(path);
}
return false;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.shiro.permissions.custom;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.permission.PermissionResolver;
import java.nio.file.Paths;
public class PathPermissionResolver implements PermissionResolver {
@Override
public Permission resolvePermission(String permissionString) {
return new PathPermission(Paths.get(permissionString));
}
}

View File

@ -0,0 +1,10 @@
[users]
jane.admin = password, admin
john.editor = password2, editor
zoe.author = password3, author
paul.reader = password4
[roles]
admin = /
editor = /articles
author = /articles/drafts

150
apache-spark/data/iris.data Normal file
View File

@ -0,0 +1,150 @@
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
5.0,3.4,1.5,0.2,Iris-setosa
4.4,2.9,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.4,3.7,1.5,0.2,Iris-setosa
4.8,3.4,1.6,0.2,Iris-setosa
4.8,3.0,1.4,0.1,Iris-setosa
4.3,3.0,1.1,0.1,Iris-setosa
5.8,4.0,1.2,0.2,Iris-setosa
5.7,4.4,1.5,0.4,Iris-setosa
5.4,3.9,1.3,0.4,Iris-setosa
5.1,3.5,1.4,0.3,Iris-setosa
5.7,3.8,1.7,0.3,Iris-setosa
5.1,3.8,1.5,0.3,Iris-setosa
5.4,3.4,1.7,0.2,Iris-setosa
5.1,3.7,1.5,0.4,Iris-setosa
4.6,3.6,1.0,0.2,Iris-setosa
5.1,3.3,1.7,0.5,Iris-setosa
4.8,3.4,1.9,0.2,Iris-setosa
5.0,3.0,1.6,0.2,Iris-setosa
5.0,3.4,1.6,0.4,Iris-setosa
5.2,3.5,1.5,0.2,Iris-setosa
5.2,3.4,1.4,0.2,Iris-setosa
4.7,3.2,1.6,0.2,Iris-setosa
4.8,3.1,1.6,0.2,Iris-setosa
5.4,3.4,1.5,0.4,Iris-setosa
5.2,4.1,1.5,0.1,Iris-setosa
5.5,4.2,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.0,3.2,1.2,0.2,Iris-setosa
5.5,3.5,1.3,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
4.4,3.0,1.3,0.2,Iris-setosa
5.1,3.4,1.5,0.2,Iris-setosa
5.0,3.5,1.3,0.3,Iris-setosa
4.5,2.3,1.3,0.3,Iris-setosa
4.4,3.2,1.3,0.2,Iris-setosa
5.0,3.5,1.6,0.6,Iris-setosa
5.1,3.8,1.9,0.4,Iris-setosa
4.8,3.0,1.4,0.3,Iris-setosa
5.1,3.8,1.6,0.2,Iris-setosa
4.6,3.2,1.4,0.2,Iris-setosa
5.3,3.7,1.5,0.2,Iris-setosa
5.0,3.3,1.4,0.2,Iris-setosa
7.0,3.2,4.7,1.4,Iris-versicolor
6.4,3.2,4.5,1.5,Iris-versicolor
6.9,3.1,4.9,1.5,Iris-versicolor
5.5,2.3,4.0,1.3,Iris-versicolor
6.5,2.8,4.6,1.5,Iris-versicolor
5.7,2.8,4.5,1.3,Iris-versicolor
6.3,3.3,4.7,1.6,Iris-versicolor
4.9,2.4,3.3,1.0,Iris-versicolor
6.6,2.9,4.6,1.3,Iris-versicolor
5.2,2.7,3.9,1.4,Iris-versicolor
5.0,2.0,3.5,1.0,Iris-versicolor
5.9,3.0,4.2,1.5,Iris-versicolor
6.0,2.2,4.0,1.0,Iris-versicolor
6.1,2.9,4.7,1.4,Iris-versicolor
5.6,2.9,3.6,1.3,Iris-versicolor
6.7,3.1,4.4,1.4,Iris-versicolor
5.6,3.0,4.5,1.5,Iris-versicolor
5.8,2.7,4.1,1.0,Iris-versicolor
6.2,2.2,4.5,1.5,Iris-versicolor
5.6,2.5,3.9,1.1,Iris-versicolor
5.9,3.2,4.8,1.8,Iris-versicolor
6.1,2.8,4.0,1.3,Iris-versicolor
6.3,2.5,4.9,1.5,Iris-versicolor
6.1,2.8,4.7,1.2,Iris-versicolor
6.4,2.9,4.3,1.3,Iris-versicolor
6.6,3.0,4.4,1.4,Iris-versicolor
6.8,2.8,4.8,1.4,Iris-versicolor
6.7,3.0,5.0,1.7,Iris-versicolor
6.0,2.9,4.5,1.5,Iris-versicolor
5.7,2.6,3.5,1.0,Iris-versicolor
5.5,2.4,3.8,1.1,Iris-versicolor
5.5,2.4,3.7,1.0,Iris-versicolor
5.8,2.7,3.9,1.2,Iris-versicolor
6.0,2.7,5.1,1.6,Iris-versicolor
5.4,3.0,4.5,1.5,Iris-versicolor
6.0,3.4,4.5,1.6,Iris-versicolor
6.7,3.1,4.7,1.5,Iris-versicolor
6.3,2.3,4.4,1.3,Iris-versicolor
5.6,3.0,4.1,1.3,Iris-versicolor
5.5,2.5,4.0,1.3,Iris-versicolor
5.5,2.6,4.4,1.2,Iris-versicolor
6.1,3.0,4.6,1.4,Iris-versicolor
5.8,2.6,4.0,1.2,Iris-versicolor
5.0,2.3,3.3,1.0,Iris-versicolor
5.6,2.7,4.2,1.3,Iris-versicolor
5.7,3.0,4.2,1.2,Iris-versicolor
5.7,2.9,4.2,1.3,Iris-versicolor
6.2,2.9,4.3,1.3,Iris-versicolor
5.1,2.5,3.0,1.1,Iris-versicolor
5.7,2.8,4.1,1.3,Iris-versicolor
6.3,3.3,6.0,2.5,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
7.1,3.0,5.9,2.1,Iris-virginica
6.3,2.9,5.6,1.8,Iris-virginica
6.5,3.0,5.8,2.2,Iris-virginica
7.6,3.0,6.6,2.1,Iris-virginica
4.9,2.5,4.5,1.7,Iris-virginica
7.3,2.9,6.3,1.8,Iris-virginica
6.7,2.5,5.8,1.8,Iris-virginica
7.2,3.6,6.1,2.5,Iris-virginica
6.5,3.2,5.1,2.0,Iris-virginica
6.4,2.7,5.3,1.9,Iris-virginica
6.8,3.0,5.5,2.1,Iris-virginica
5.7,2.5,5.0,2.0,Iris-virginica
5.8,2.8,5.1,2.4,Iris-virginica
6.4,3.2,5.3,2.3,Iris-virginica
6.5,3.0,5.5,1.8,Iris-virginica
7.7,3.8,6.7,2.2,Iris-virginica
7.7,2.6,6.9,2.3,Iris-virginica
6.0,2.2,5.0,1.5,Iris-virginica
6.9,3.2,5.7,2.3,Iris-virginica
5.6,2.8,4.9,2.0,Iris-virginica
7.7,2.8,6.7,2.0,Iris-virginica
6.3,2.7,4.9,1.8,Iris-virginica
6.7,3.3,5.7,2.1,Iris-virginica
7.2,3.2,6.0,1.8,Iris-virginica
6.2,2.8,4.8,1.8,Iris-virginica
6.1,3.0,4.9,1.8,Iris-virginica
6.4,2.8,5.6,2.1,Iris-virginica
7.2,3.0,5.8,1.6,Iris-virginica
7.4,2.8,6.1,1.9,Iris-virginica
7.9,3.8,6.4,2.0,Iris-virginica
6.4,2.8,5.6,2.2,Iris-virginica
6.3,2.8,5.1,1.5,Iris-virginica
6.1,2.6,5.6,1.4,Iris-virginica
7.7,3.0,6.1,2.3,Iris-virginica
6.3,3.4,5.6,2.4,Iris-virginica
6.4,3.1,5.5,1.8,Iris-virginica
6.0,3.0,4.8,1.8,Iris-virginica
6.9,3.1,5.4,2.1,Iris-virginica
6.7,3.1,5.6,2.4,Iris-virginica
6.9,3.1,5.1,2.3,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
6.8,3.2,5.9,2.3,Iris-virginica
6.7,3.3,5.7,2.5,Iris-virginica
6.7,3.0,5.2,2.3,Iris-virginica
6.3,2.5,5.0,1.9,Iris-virginica
6.5,3.0,5.2,2.0,Iris-virginica
6.2,3.4,5.4,2.3,Iris-virginica
5.9,3.0,5.1,1.8,Iris-virginica

View File

@ -0,0 +1 @@
{"class":"org.apache.spark.mllib.classification.LogisticRegressionModel","version":"1.0","numFeatures":4,"numClasses":3}

View File

@ -1,31 +1,32 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>apache-spark</artifactId>
<version>1.0-SNAPSHOT</version>
<name>apache-spark</name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>apache-spark</artifactId>
<version>1.0-SNAPSHOT</version>
<name>apache-spark</name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>${org.apache.spark.spark-core.version}</version>
<scope>provided</scope>
</dependency>
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>${org.apache.spark.spark-sql.version}</version>
<scope>provided</scope>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>${org.apache.spark.spark-core.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>${org.apache.spark.spark-sql.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
@ -33,6 +34,12 @@
<version>${org.apache.spark.spark-streaming.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-mllib_2.11</artifactId>
<version>${org.apache.spark.spark-mllib.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-kafka-0-10_2.11</artifactId>
@ -48,46 +55,47 @@
<artifactId>spark-cassandra-connector-java_2.11</artifactId>
<version>${com.datastax.spark.spark-cassandra-connector-java.version}</version>
</dependency>
</dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<org.apache.spark.spark-core.version>2.3.0</org.apache.spark.spark-core.version>
<org.apache.spark.spark-sql.version>2.3.0</org.apache.spark.spark-sql.version>
<org.apache.spark.spark-streaming.version>2.3.0</org.apache.spark.spark-streaming.version>
<org.apache.spark.spark-streaming-kafka.version>2.3.0</org.apache.spark.spark-streaming-kafka.version>
<com.datastax.spark.spark-cassandra-connector.version>2.3.0</com.datastax.spark.spark-cassandra-connector.version>
<com.datastax.spark.spark-cassandra-connector-java.version>1.5.2</com.datastax.spark.spark-cassandra-connector-java.version>
<properties>
<org.apache.spark.spark-core.version>2.3.0</org.apache.spark.spark-core.version>
<org.apache.spark.spark-sql.version>2.3.0</org.apache.spark.spark-sql.version>
<org.apache.spark.spark-streaming.version>2.3.0</org.apache.spark.spark-streaming.version>
<org.apache.spark.spark-mllib.version>2.3.0</org.apache.spark.spark-mllib.version>
<org.apache.spark.spark-streaming-kafka.version>2.3.0</org.apache.spark.spark-streaming-kafka.version>
<com.datastax.spark.spark-cassandra-connector.version>2.3.0</com.datastax.spark.spark-cassandra-connector.version>
<com.datastax.spark.spark-cassandra-connector-java.version>1.5.2</com.datastax.spark.spark-cassandra-connector-java.version>
<maven-compiler-plugin.version>3.2</maven-compiler-plugin.version>
</properties>
</properties>
</project>

View File

@ -0,0 +1,111 @@
package com.baeldung.ml;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.mllib.classification.LogisticRegressionModel;
import org.apache.spark.mllib.classification.LogisticRegressionWithLBFGS;
import org.apache.spark.mllib.evaluation.MulticlassMetrics;
import org.apache.spark.mllib.linalg.Matrix;
import org.apache.spark.mllib.linalg.Vector;
import org.apache.spark.mllib.linalg.Vectors;
import org.apache.spark.mllib.regression.LabeledPoint;
import org.apache.spark.mllib.stat.MultivariateStatisticalSummary;
import org.apache.spark.mllib.stat.Statistics;
import scala.Tuple2;
public class MachineLearningApp {
public static void main(String[] args) {
// 1. Setting the Spark Context
SparkConf conf = new SparkConf().setAppName("Main")
.setMaster("local[2]")
.set("spark.executor.memory", "3g")
.set("spark.driver.memory", "3g");
JavaSparkContext sc = new JavaSparkContext(conf);
Logger.getLogger("org")
.setLevel(Level.OFF);
Logger.getLogger("akka")
.setLevel(Level.OFF);
// 2. Loading the Data-set
String dataFile = "data\\iris.data";
JavaRDD<String> data = sc.textFile(dataFile);
// 3. Exploratory Data Analysis
// 3.1. Creating Vector of Input Data
JavaRDD<Vector> inputData = data.map(line -> {
String[] parts = line.split(",");
double[] v = new double[parts.length - 1];
for (int i = 0; i < parts.length - 1; i++) {
v[i] = Double.parseDouble(parts[i]);
}
return Vectors.dense(v);
});
// 3.2. Performing Statistical Analysis
MultivariateStatisticalSummary summary = Statistics.colStats(inputData.rdd());
System.out.println("Summary Mean:");
System.out.println(summary.mean());
System.out.println("Summary Variance:");
System.out.println(summary.variance());
System.out.println("Summary Non-zero:");
System.out.println(summary.numNonzeros());
// 3.3. Performing Correlation Analysis
Matrix correlMatrix = Statistics.corr(inputData.rdd(), "pearson");
System.out.println("Correlation Matrix:");
System.out.println(correlMatrix.toString());
// 4. Data Preparation
// 4.1. Creating Map for Textual Output Labels
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Iris-setosa", 0);
map.put("Iris-versicolor", 1);
map.put("Iris-virginica", 2);
// 4.2. Creating LabeledPoint of Input and Output Data
JavaRDD<LabeledPoint> parsedData = data.map(line -> {
String[] parts = line.split(",");
double[] v = new double[parts.length - 1];
for (int i = 0; i < parts.length - 1; i++) {
v[i] = Double.parseDouble(parts[i]);
}
return new LabeledPoint(map.get(parts[parts.length - 1]), Vectors.dense(v));
});
// 5. Data Splitting into 80% Training and 20% Test Sets
JavaRDD<LabeledPoint>[] splits = parsedData.randomSplit(new double[] { 0.8, 0.2 }, 11L);
JavaRDD<LabeledPoint> trainingData = splits[0].cache();
JavaRDD<LabeledPoint> testData = splits[1];
// 6. Modeling
// 6.1. Model Training
LogisticRegressionModel model = new LogisticRegressionWithLBFGS().setNumClasses(3)
.run(trainingData.rdd());
// 6.2. Model Evaluation
JavaPairRDD<Object, Object> predictionAndLabels = testData.mapToPair(p -> new Tuple2<>(model.predict(p.features()), p.label()));
MulticlassMetrics metrics = new MulticlassMetrics(predictionAndLabels.rdd());
double accuracy = metrics.accuracy();
System.out.println("Model Accuracy on Test Data: " + accuracy);
// 7. Model Saving and Loading
// 7.1. Model Saving
model.save(sc.sc(), "model\\logistic-regression");
// 7.2. Model Loading
LogisticRegressionModel sameModel = LogisticRegressionModel.load(sc.sc(), "model\\logistic-regression");
// 7.3. Prediction on New Data
Vector newData = Vectors.dense(new double[] { 1, 1, 1, 1 });
double prediction = sameModel.predict(newData);
System.out.println("Model Prediction on New Data = " + prediction);
// 8. Clean-up
sc.close();
}
}

3
bazel/README.md Normal file
View File

@ -0,0 +1,3 @@
## Relevant Articles:
- [Building Java Applications with Bazel](https://www.baeldung.com/bazel-build-tool)

31
bazel/WORKSPACE Normal file
View File

@ -0,0 +1,31 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
RULES_JVM_EXTERNAL_TAG = "2.0.1"
RULES_JVM_EXTERNAL_SHA = "55e8d3951647ae3dffde22b4f7f8dee11b3f70f3f89424713debd7076197eaca"
http_archive(
name = "rules_jvm_external",
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
sha256 = RULES_JVM_EXTERNAL_SHA,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)
load("@rules_jvm_external//:defs.bzl", "maven_install")
maven_install(
artifacts = [
"org.apache.commons:commons-lang3:3.9"
],
repositories = [
"https://repo1.maven.org/maven2",
]
)
http_jar (
name = "apache-commons-lang",
url = "https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar"
)

7
bazel/bazelapp/BUILD Normal file
View File

@ -0,0 +1,7 @@
java_binary (
name = "BazelApp",
srcs = glob(["src/main/java/com/baeldung/*.java"]),
main_class = "com.baeldung.BazelApp",
deps = ["//bazelgreeting:greeter", "@maven//:org_apache_commons_commons_lang3"]
)

29
bazel/bazelapp/pom.xml Normal file
View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>bazelapp</artifactId>
<name>bazelapp</name>
<parent>
<artifactId>bazel</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.baeldung</groupId>
<artifactId>bazelgreeting</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,15 @@
package com.baeldung;
import com.baeldung.Greetings;
import org.apache.commons.lang3.StringUtils;
public class BazelApp {
public static void main(String ... args) {
Greetings greetings = new Greetings();
System.out.println(greetings.greet("Bazel"));
System.out.println(StringUtils.lowerCase("Bazel"));
}
}

View File

@ -0,0 +1,6 @@
java_library (
name = "greeter",
srcs = glob(["src/main/java/com/baeldung/*.java"]),
visibility = ["//bazelapp:__pkg__"]
)

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>bazelgreeting</artifactId>
<name>bazelgreeting</name>
<parent>
<artifactId>bazel</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,8 @@
package com.baeldung;
public class Greetings {
public String greet(String name) {
return "Hello ".concat(name);
}
}

22
bazel/pom.xml Normal file
View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>bazel</artifactId>
<packaging>pom</packaging>
<name>bazel</name>
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modules>
<module>bazelgreeting</module>
<module>bazelapp</module>
</modules>
</project>

View File

@ -8,3 +8,4 @@
- [Pattern Matching in Strings in Groovy](https://www.baeldung.com/groovy-pattern-matching)
- [Working with XML in Groovy](https://www.baeldung.com/groovy-xml)
- [Integrating Groovy into Java Applications](https://www.baeldung.com/groovy-java-applications)
- [Concatenate Strings with Groovy](https://www.baeldung.com/groovy-concatenate-strings)

View File

@ -48,6 +48,11 @@
<version>${spock-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.groovy-wslite</groupId>
<artifactId>groovy-wslite</artifactId>
<version>${groovy-wslite.version}</version>
</dependency>
</dependencies>
<build>
@ -175,6 +180,7 @@
<junit.platform.version>1.0.0</junit.platform.version>
<hsqldb.version>2.4.0</hsqldb.version>
<spock-core.version>1.1-groovy-2.4</spock-core.version>
<groovy-wslite.version>1.1.3</groovy-wslite.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<logback.version>1.2.3</logback.version>
<groovy.version>2.5.7</groovy.version>

View File

@ -0,0 +1,152 @@
package com.baeldung.webservice
import groovy.json.JsonSlurper
import wslite.rest.ContentType
import wslite.rest.RESTClient
import wslite.rest.RESTClientException
import wslite.soap.SOAPClient
import wslite.soap.SOAPMessageBuilder
import wslite.http.auth.HTTPBasicAuthorization
import org.junit.Test
class WebserviceUnitTest extends GroovyTestCase {
JsonSlurper jsonSlurper = new JsonSlurper()
static RESTClient client = new RESTClient("https://postman-echo.com")
static {
client.defaultAcceptHeader = ContentType.JSON
client.httpClient.sslTrustAllCerts = true
}
void test_whenSendingGet_thenRespose200() {
def postmanGet = new URL('https://postman-echo.com/get')
def getConnection = postmanGet.openConnection()
getConnection.requestMethod = 'GET'
assert getConnection.responseCode == 200
if (getConnection.responseCode == 200) {
assert jsonSlurper.parseText(getConnection.content.text)?.headers?.host == "postman-echo.com"
}
}
void test_whenSendingPost_thenRespose200() {
def postmanPost = new URL('https://postman-echo.com/post')
def query = "q=This is post request form parameter."
def postConnection = postmanPost.openConnection()
postConnection.requestMethod = 'POST'
assert postConnection.responseCode == 200
}
void test_whenSendingPostWithParams_thenRespose200() {
def postmanPost = new URL('https://postman-echo.com/post')
def form = "param1=This is request parameter."
def postConnection = postmanPost.openConnection()
postConnection.requestMethod = 'POST'
postConnection.doOutput = true
def text
postConnection.with {
outputStream.withWriter { outputStreamWriter ->
outputStreamWriter << form
}
text = content.text
}
assert postConnection.responseCode == 200
assert jsonSlurper.parseText(text)?.json.param1 == "This is request parameter."
}
void test_whenReadingRSS_thenReceiveFeed() {
def rssFeed = new XmlParser().parse("https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en")
def stories = []
(0..4).each {
def item = rssFeed.channel.item.get(it)
stories << item.title.text()
}
assert stories.size() == 5
}
void test_whenReadingAtom_thenReceiveFeed() {
def atomFeed = new XmlParser().parse("https://news.google.com/atom?hl=en-US&gl=US&ceid=US:en")
def stories = []
(0..4).each {
def entry = atomFeed.entry.get(it)
stories << entry.title.text()
}
assert stories.size() == 5
}
void test_whenConsumingSoap_thenReceiveResponse() {
def url = "http://www.dataaccess.com/webservicesserver/numberconversion.wso"
def soapClient = new SOAPClient(url)
def message = new SOAPMessageBuilder().build({
body {
NumberToWords(xmlns: "http://www.dataaccess.com/webservicesserver/") {
ubiNum(1234)
}
}
})
def response = soapClient.send(message.toString());
def words = response.NumberToWordsResponse
assert words == "one thousand two hundred and thirty four "
}
void test_whenConsumingRestGet_thenReceiveResponse() {
def path = "/get"
def response
try {
response = client.get(path: path)
assert response.statusCode == 200
assert response.json?.headers?.host == "postman-echo.com"
} catch (RESTClientException e) {
assert e?.response?.statusCode != 200
}
}
void test_whenConsumingRestPost_thenReceiveResponse() {
def path = "/post"
def params = ["foo":1,"bar":2]
def response
try {
response = client.post(path: path) {
type ContentType.JSON
json params
}
assert response.json?.data == params
} catch (RESTClientException e) {
e.printStackTrace()
assert e?.response?.statusCode != 200
}
}
void test_whenBasicAuthentication_thenReceive200() {
def path = "/basic-auth"
def response
try {
client.authorization = new HTTPBasicAuthorization("postman", "password")
response = client.get(path: path)
assert response.statusCode == 200
assert response.json?.authenticated == true
} catch (RESTClientException e) {
e.printStackTrace()
assert e?.response?.statusCode != 200
}
}
void test_whenOAuth_thenReceive200() {
RESTClient oAuthClient = new RESTClient("https://postman-echo.com")
oAuthClient.defaultAcceptHeader = ContentType.JSON
oAuthClient.httpClient.sslTrustAllCerts = true
def path = "/oauth1"
def params = [oauth_consumer_key: "RKCGzna7bv9YD57c", oauth_signature_method: "HMAC-SHA1", oauth_timestamp:1567089944, oauth_nonce: "URT7v4", oauth_version: 1.0, oauth_signature: 'RGgR/ktDmclkM0ISWaFzebtlO0A=']
def response
try {
response = oAuthClient.get(path: path, query: params)
assert response.statusCode == 200
assert response.statusMessage == "OK"
assert response.json.status == "pass"
} catch (RESTClientException e) {
assert e?.response?.statusCode != 200
}
}
}

View File

@ -20,7 +20,6 @@
- [Finding Min/Max in an Array with Java](http://www.baeldung.com/java-array-min-max)
- [Internationalization and Localization in Java 8](http://www.baeldung.com/java-8-localization)
- [Java Optional orElse() vs orElseGet()](http://www.baeldung.com/java-optional-or-else-vs-or-else-get)
- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection)
- [Java 8 Unsigned Arithmetic Support](http://www.baeldung.com/java-unsigned-arithmetic)
- [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference)
- [Overriding System Time for Testing in Java](http://www.baeldung.com/java-override-system-time)

View File

@ -134,16 +134,6 @@
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
@ -192,7 +182,6 @@
<jmh-generator.version>1.19</jmh-generator.version>
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
<!-- plugins -->
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
</properties>
</project>

View File

@ -1,4 +1,4 @@
package com.baeldung.java8;
package com.baeldung.java_8_features.groupingby;
import com.baeldung.java_8_features.groupingby.BlogPost;
import com.baeldung.java_8_features.groupingby.BlogPostType;

View File

@ -1,3 +1,5 @@
## Relevant Articles
- [Extending an Arrays Length](https://www.baeldung.com/java-array-add-element-at-the-end)
- [Checking If an Array Is Sorted in Java](https://www.baeldung.com/java-check-sorted-array)
- [Looping Diagonally Through a 2d Java Array](https://www.baeldung.com/java-loop-diagonal-array)

View File

@ -0,0 +1,40 @@
package com.baeldung.java.list;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class RemoveFromList {
public static void main(String[] args) {
List<String> sports = new ArrayList<>();
sports.add("Football");
sports.add("Basketball");
sports.add("Baseball");
sports.add("Boxing");
sports.add("Cycling");
System.out.println("List before removing: " + sports);
// Remove with index
sports.remove(1);
// Remove with an element
sports.remove("Baseball");
// Iterator remove method
Iterator<String> iterator = sports.iterator();
while (iterator.hasNext()) {
if (iterator.next().equals("Boxing")) {
iterator.remove();
break;
}
}
// ArrayList removeIf method (Java 8)
sports.removeIf(p -> p.equals("Cycling"));
System.out.println("List after removing: " + sports);
}
}

View File

@ -10,8 +10,4 @@
- [Java List Initialization in One Line](https://www.baeldung.com/java-init-list-one-line)
- [Ways to Iterate Over a List in Java](https://www.baeldung.com/java-iterate-list)
- [Flattening Nested Collections in Java](http://www.baeldung.com/java-flatten-nested-collections)
- [Intersection of Two Lists in Java](https://www.baeldung.com/java-lists-intersection)
- [Determine If All Elements Are the Same in a Java List](https://www.baeldung.com/java-list-all-equal)
- [List of Primitive Integer Values in Java](https://www.baeldung.com/java-list-primitive-int)
- [Performance Comparison of Primitive Lists in Java](https://www.baeldung.com/java-list-primitive-performance)
- [Filtering a Java Collection by a List](https://www.baeldung.com/java-filter-collection-by-list)
- [Intersection of Two Lists in Java](https://www.baeldung.com/java-lists-intersection)

View File

@ -36,41 +36,11 @@
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sf.trove4j</groupId>
<artifactId>trove4j</artifactId>
<version>${trove4j.version}</version>
</dependency>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>${fastutil.version}</version>
</dependency>
<dependency>
<groupId>colt</groupId>
<artifactId>colt</artifactId>
<version>${colt.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-core.version}</version>
</dependency>
</dependencies>
<properties>
<commons-collections4.version>4.1</commons-collections4.version>
<commons-lang3.version>3.8.1</commons-lang3.version>
<assertj.version>3.11.1</assertj.version>
<trove4j.version>3.0.2</trove4j.version>
<fastutil.version>8.1.0</fastutil.version>
<colt.version>1.2.0</colt.version>
</properties>
</project>

View File

@ -42,7 +42,7 @@ public class ListInitializationUnitTest {
}
@Test
public void givenArrayAsList_whenCreated_thenShareReference() {
public void givenArraysAsList_whenCreated_thenShareReference() {
String[] array = { "foo", "bar" };
List<String> list = Arrays.asList(array);
array[0] = "baz";

View File

@ -0,0 +1,11 @@
=========
## Core Java Collections List Cookbooks and Examples
### Relevant Articles:
- [Collections.emptyList() vs. New List Instance](https://www.baeldung.com/java-collections-emptylist-new-list)
- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another)
- [Determine If All Elements Are the Same in a Java List](https://www.baeldung.com/java-list-all-equal)
- [List of Primitive Integer Values in Java](https://www.baeldung.com/java-list-primitive-int)
- [Performance Comparison of Primitive Lists in Java](https://www.baeldung.com/java-list-primitive-performance)
- [Filtering a Java Collection by a List](https://www.baeldung.com/java-filter-collection-by-list)

View File

@ -0,0 +1,76 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-list-3</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-collections-list-3</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sf.trove4j</groupId>
<artifactId>trove4j</artifactId>
<version>${trove4j.version}</version>
</dependency>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>${fastutil.version}</version>
</dependency>
<dependency>
<groupId>colt</groupId>
<artifactId>colt</artifactId>
<version>${colt.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-core.version}</version>
</dependency>
</dependencies>
<properties>
<commons-collections4.version>4.1</commons-collections4.version>
<commons-lang3.version>3.8.1</commons-lang3.version>
<assertj.version>3.11.1</assertj.version>
<trove4j.version>3.0.2</trove4j.version>
<fastutil.version>8.1.0</fastutil.version>
<colt.version>1.2.0</colt.version>
</properties>
</project>

View File

@ -10,7 +10,5 @@
- [Iterating Backward Through a List](http://www.baeldung.com/java-list-iterate-backwards)
- [Remove the First Element from a List](http://www.baeldung.com/java-remove-first-element-from-list)
- [How to Find an Element in a List with Java](http://www.baeldung.com/find-list-element-java)
- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another)
- [Finding Max/Min of a List or Collection](http://www.baeldung.com/java-collection-min-max)
- [Collections.emptyList() vs. New List Instance](https://www.baeldung.com/java-collections-emptylist-new-list)
- [Remove All Occurrences of a Specific Value from a List](https://www.baeldung.com/java-remove-value-from-list)

View File

@ -0,0 +1,26 @@
*.class
0.*
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
.resourceCache
# Packaged files #
*.jar
*.war
*.ear
# Files generated by integration tests
*.txt
backup-pom.xml
/bin/
/temp
#IntelliJ specific
.idea/
*.iml

View File

@ -0,0 +1,7 @@
=========
## Core Java Concurrency 2 Examples
### Relevant Articles:
- [Using a Mutex Object in Java](https://www.baeldung.com/java-mutex)

View File

@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>core-java-concurrency-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-concurrency-2</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<build>
<finalName>core-java-concurrency-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>

View File

@ -0,0 +1,12 @@
package com.baeldung.concurrent.mutex;
public class SequenceGenerator {
private int currentValue = 0;
public int getNextSequence() {
currentValue = currentValue + 1;
return currentValue;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.concurrent.mutex;
import com.google.common.util.concurrent.Monitor;
public class SequenceGeneratorUsingMonitor extends SequenceGenerator {
private Monitor mutex = new Monitor();
@Override
public int getNextSequence() {
mutex.enter();
try {
return super.getNextSequence();
} finally {
mutex.leave();
}
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.concurrent.mutex;
import java.util.concurrent.locks.ReentrantLock;
public class SequenceGeneratorUsingReentrantLock extends SequenceGenerator {
private ReentrantLock mutex = new ReentrantLock();
@Override
public int getNextSequence() {
try {
mutex.lock();
return super.getNextSequence();
} finally {
mutex.unlock();
}
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.concurrent.mutex;
import java.util.concurrent.Semaphore;
public class SequenceGeneratorUsingSemaphore extends SequenceGenerator {
private Semaphore mutex = new Semaphore(1);
@Override
public int getNextSequence() {
try {
mutex.acquire();
return super.getNextSequence();
} catch (InterruptedException e) {
throw new RuntimeException("Exception in critical section.", e);
} finally {
mutex.release();
}
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.concurrent.mutex;
public class SequenceGeneratorUsingSynchronizedBlock extends SequenceGenerator {
private Object mutex = new Object();
@Override
public int getNextSequence() {
synchronized (mutex) {
return super.getNextSequence();
}
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.concurrent.mutex;
public class SequenceGeneratorUsingSynchronizedMethod extends SequenceGenerator {
@Override
public synchronized int getNextSequence() {
return super.getNextSequence();
}
}

View File

@ -2,7 +2,7 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>

View File

@ -0,0 +1,80 @@
package com.baeldung.concurrent.mutex;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.Test;
public class MutexUnitTest {
// @Test
// This test verifies the race condition use case, it may pass or fail based on execution environment
// Uncomment @Test to run it
public void givenUnsafeSequenceGenerator_whenRaceCondition_thenUnexpectedBehavior() throws Exception {
int count = 1000;
Set<Integer> uniqueSequences = getUniqueSequences(new SequenceGenerator(), count);
Assert.assertNotEquals(count, uniqueSequences.size());
}
@Test
public void givenSequenceGeneratorUsingSynchronizedMethod_whenRaceCondition_thenSuccess() throws Exception {
int count = 1000;
Set<Integer> uniqueSequences = getUniqueSequences(new SequenceGeneratorUsingSynchronizedMethod(), count);
Assert.assertEquals(count, uniqueSequences.size());
}
@Test
public void givenSequenceGeneratorUsingSynchronizedBlock_whenRaceCondition_thenSuccess() throws Exception {
int count = 1000;
Set<Integer> uniqueSequences = getUniqueSequences(new SequenceGeneratorUsingSynchronizedBlock(), count);
Assert.assertEquals(count, uniqueSequences.size());
}
@Test
public void givenSequenceGeneratorUsingReentrantLock_whenRaceCondition_thenSuccess() throws Exception {
int count = 1000;
Set<Integer> uniqueSequences = getUniqueSequences(new SequenceGeneratorUsingReentrantLock(), count);
Assert.assertEquals(count, uniqueSequences.size());
}
@Test
public void givenSequenceGeneratorUsingSemaphore_whenRaceCondition_thenSuccess() throws Exception {
int count = 1000;
Set<Integer> uniqueSequences = getUniqueSequences(new SequenceGeneratorUsingSemaphore(), count);
Assert.assertEquals(count, uniqueSequences.size());
}
@Test
public void givenSequenceGeneratorUsingMonitor_whenRaceCondition_thenSuccess() throws Exception {
int count = 1000;
Set<Integer> uniqueSequences = getUniqueSequences(new SequenceGeneratorUsingMonitor(), count);
Assert.assertEquals(count, uniqueSequences.size());
}
private Set<Integer> getUniqueSequences(SequenceGenerator generator, int count) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(3);
Set<Integer> uniqueSequences = new LinkedHashSet<>();
List<Future<Integer>> futures = new ArrayList<>();
for (int i = 0; i < count; i++) {
futures.add(executor.submit(generator::getNextSequence));
}
for (Future<Integer> future : futures) {
uniqueSequences.add(future.get());
}
executor.awaitTermination(1, TimeUnit.SECONDS);
executor.shutdown();
return uniqueSequences;
}
}

View File

@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear

View File

@ -1,4 +1,4 @@
package com.baeldung.java8;
package com.baeldung.concurrent.executorservice;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

View File

@ -0,0 +1,43 @@
package com.baeldung.exceptions;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class CheckedUncheckedExceptions {
public static void checkedExceptionWithThrows() throws FileNotFoundException {
File file = new File("not_existing_file.txt");
FileInputStream stream = new FileInputStream(file);
}
public static void checkedExceptionWithTryCatch() {
File file = new File("not_existing_file.txt");
try {
FileInputStream stream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static int divideByZero() {
int numerator = 1;
int denominator = 0;
return numerator / denominator;
}
public static void checkFile(String fileName) throws IncorrectFileNameException {
if (fileName == null || fileName.isEmpty()) {
throw new NullOrEmptyException("The filename is null.");
}
if (!isCorrectFileName(fileName)) {
throw new IncorrectFileNameException("Incorrect filename : " + fileName);
}
}
private static boolean isCorrectFileName(String fileName) {
if (fileName.equals("wrongFileName.txt"))
return false;
else
return true;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.exceptions;
public class IncorrectFileNameException extends Exception {
private static final long serialVersionUID = 1L;
public IncorrectFileNameException(String errorMessage) {
super(errorMessage);
}
public IncorrectFileNameException(String errorMessage, Throwable thr) {
super(errorMessage, thr);
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.exceptions;
public class NullOrEmptyException extends RuntimeException {
private static final long serialVersionUID = 1L;
public NullOrEmptyException(String errorMessage) {
super(errorMessage);
}
public NullOrEmptyException(String errorMessage, Throwable thr) {
super(errorMessage, thr);
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.exceptions;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.FileNotFoundException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* Tests the {@link CheckedUncheckedExceptions}.
*/
public class CheckedUncheckedExceptionsUnitTest {
@Test
public void whenFileNotExist_thenThrowException() {
assertThrows(FileNotFoundException.class, () -> {
CheckedUncheckedExceptions.checkedExceptionWithThrows();
});
}
@Test
public void whenTryCatchExcetpion_thenSuccess() {
try {
CheckedUncheckedExceptions.checkedExceptionWithTryCatch();
} catch (Exception e) {
Assertions.fail(e.getMessage());
}
}
@Test
public void whenDivideByZero_thenThrowException() {
assertThrows(ArithmeticException.class, () -> {
CheckedUncheckedExceptions.divideByZero();
});
}
@Test
public void whenInvalidFile_thenThrowException() {
assertThrows(IncorrectFileNameException.class, () -> {
CheckedUncheckedExceptions.checkFile("wrongFileName.txt");
});
}
@Test
public void whenNullOrEmptyFile_thenThrowException() {
assertThrows(NullOrEmptyException.class, () -> {
CheckedUncheckedExceptions.checkFile(null);
});
assertThrows(NullOrEmptyException.class, () -> {
CheckedUncheckedExceptions.checkFile("");
});
}
}

View File

@ -0,0 +1,3 @@
# Intellij
.idea/
*.iml

Some files were not shown because too many files have changed in this diff Show More