code-snippets

Some maybe useful snippets of code
git clone https://git.ortlepp.eu/code-snippets.git/
Log | Files | Refs | README

commit 8aff53a757d539b765946b552c009d5b466442fa
parent 651e924502c5ab631cb3d379d4c8054f36ab0cfa
Author: Thorsten Ortlepp <post@ortlepp.eu>
Date:   Sat, 18 Apr 2020 23:22:05 +0200

Some old Java stuff

Diffstat:
MREADME | 23++++++++++++++++++++---
Ajava/Comparator/Comparator.java | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ajava/Comparator/build.xml | 45+++++++++++++++++++++++++++++++++++++++++++++
Ajava/DuplicatedLines/DuplicatedLines.java | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ajava/DuplicatedLines/build.xml | 45+++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 288 insertions(+), 3 deletions(-)

diff --git a/README b/README @@ -1,10 +1,27 @@ -# Code Snippets +Code Snippets +============= This repository contains some maybe useful snippets of code. +Feel free to use (and modify) the scripts and programs. -## PHP -### Heise RSS +Java +==== +Comparator +---------- +A simple program that compares two text files and lists either identical or differing lines. + +DuplicatedLines +--------------- +A simple program that finds and lists all duplicated lines in a text file. + + + +PHP +=== + +Heise RSS +--------- A script to remove unwanted articles from Heise Online RSS feed. diff --git a/java/Comparator/Comparator.java b/java/Comparator/Comparator.java @@ -0,0 +1,101 @@ +package eu.ortlepp.comparator; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +/** + * A simple comparator for two text files. + */ +public final class Comparator { + + /** The content of the first file. */ + private final List<String> linesFile1; + + /** The content of the second file. */ + private final List<String> linesFile2; + + + /** + * Starts the program and checks the params. + * + * @param args Params for the program; expected: 1. comparison 2. first file 3. second file + * @throws IOException Error while reading the files + */ + public static void main(String[] args) throws IOException { + if (args.length == 3) { + + if (args[0].equalsIgnoreCase("--matches")) { + new Comparator(args[1], args[2]).compareMatches(); + } else if (args[0].equalsIgnoreCase("--differences")) { + new Comparator(args[1], args[2]).compareDifferences(); + } else { + System.out.println("Unknown comparison, use --matches or --differences"); + } + + } else { + System.out.println("Parameters: [--matches|--differences] [first file] [second file]"); + } + } + + + /** + * Read the content of the files and remove empty lines. + * + * @param paramFile1 The name (and path) of the first file + * @param paramFile2 The name (and path) of the second file + * @throws IOException Error while reading the files + */ + private Comparator(String paramFile1, String paramFile2) throws IOException { + linesFile1 = Files.readAllLines(Paths.get(paramFile1), StandardCharsets.UTF_8); + linesFile1.removeIf(e -> e.isEmpty()); + linesFile2 = Files.readAllLines(Paths.get(paramFile2), StandardCharsets.UTF_8); + linesFile2.removeIf(e -> e.isEmpty()); + } + + + /** + * Compare the two files; all lines that are in the first file as well as in the second file are printed out. + */ + private void compareMatches() { + System.out.println("# Lines in first and second file:"); + linesFile1.retainAll(linesFile2); + printLines(linesFile1); + } + + + /** + * Compare the two files; first all lines that are present only in the first are printed, + * then the lines that are present in the second file only are printed. + */ + private void compareDifferences() { + System.out.println("# Lines in first file only:"); + + List<String> temp = new ArrayList<String>(); + temp.addAll(linesFile1); + + temp.removeAll(linesFile2); + printLines(temp); + + System.out.println("\n# Lines in second file only:"); + linesFile2.removeAll(linesFile1); + printLines(linesFile2); + } + + + /** + * Print out the content of a list of strings. + * Each list item is printed in one line. + * + * @param lines The list to print out + */ + private void printLines(List<String> lines) { + for (String line : lines) { + System.out.println(line); + } + } + +} diff --git a/java/Comparator/build.xml b/java/Comparator/build.xml @@ -0,0 +1,45 @@ +<?xml version="1.0"?> +<project name="Comparator" default="jar" basedir="."> + + <property name="build.dir" location="bin" /> + <property name="dist.dir" location="dist" /> + <property name="doc.dir" location="doc" /> + <property name="jar.file" value="Comparator.jar" /> + <property name="class.main" value="eu.ortlepp.comparator.Comparator" /> + + + <target name="clean"> + <delete dir="${build.dir}" /> + <delete dir="${dist.dir}" /> + <delete dir="${doc.dir}" /> + </target> + + + <target name="compile"> + <delete dir="${build.dir}" /> + <mkdir dir="${build.dir}" /> + <javac srcdir="." destdir="${build.dir}" includeantruntime="false" /> + </target> + + + <target name="jar" depends="compile"> + <delete dir="${dist.dir}" /> + <mkdir dir="${dist.dir}" /> + <jar destfile="${dist.dir}/${jar.file}"> + <fileset dir="${build.dir}" /> + <manifest> + <attribute name="Main-Class" value="${class.main}" /> + </manifest> + </jar> + </target> + + + <target name="doc"> + <delete dir="${doc.dir}" /> + <mkdir dir="${doc.dir}" /> + <javadoc destdir="${doc.dir}" private="true" nohelp="true"> + <fileset dir="." /> + </javadoc> + </target> + +</project> diff --git a/java/DuplicatedLines/DuplicatedLines.java b/java/DuplicatedLines/DuplicatedLines.java @@ -0,0 +1,77 @@ +package eu.ortlepp.lines; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * A simple program to find duplicated lines in a text file. + */ +public class DuplicatedLines { + + /** The content of the file. */ + private final List<String> content; + + + /** + * Starts the program and checks the parameters. + * + * @param args Parameters for the program; expected: file to analyze + * @throws IOException Error while reading the file + */ + public static void main(final String[] args) throws IOException { + if (args.length == 1) { + new DuplicatedLines(args[0]).listDuplicatedLines(); + } else { + System.out.println("First (and only) parameter: File to analyze"); + } + } + + + /** + * Read the content of the file. + * + * @param file The name (and path) of the file to analyze + * @throws IOException Error while reading the file + */ + private DuplicatedLines(final String file) throws IOException { + content = Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8); + } + + + /** + * Find duplicated lines in the file; empty lines are ignored. All duplicated lines are printed out. + */ + private void listDuplicatedLines() { + Map<String, List<Integer>> uniqueLines = new HashMap<>(); + + /* Scan all lines for duplicates, ignore empty lines */ + for (int i = 0; i < content.size(); i++) { + if (!content.get(i).isEmpty()) { + if (uniqueLines.containsKey(content.get(i))) { + List<Integer> lines = uniqueLines.get(content.get(i)); + lines.add(Integer.valueOf(i + 1)); + uniqueLines.replace(content.get(i), lines); + } else { + List<Integer> lines = new ArrayList<Integer>(); + lines.add(Integer.valueOf(i + 1)); + uniqueLines.put(content.get(i), lines); + } + } + } + + /* Print duplicated lines */ + for (Map.Entry<String, List<Integer>> entry : uniqueLines.entrySet()) { + if (entry.getValue().size() > 1) { + System.out.printf("\"%s\" : %s%n", entry.getKey(), Arrays.toString(entry.getValue().toArray())); + } + } + } + +} diff --git a/java/DuplicatedLines/build.xml b/java/DuplicatedLines/build.xml @@ -0,0 +1,45 @@ +<?xml version="1.0"?> +<project name="DuplicatedLines" default="jar" basedir="."> + + <property name="build.dir" location="bin" /> + <property name="dist.dir" location="dist" /> + <property name="doc.dir" location="doc" /> + <property name="jar.file" value="DuplicatedLines.jar" /> + <property name="class.main" value="eu.ortlepp.lines.DuplicatedLines" /> + + + <target name="clean"> + <delete dir="${build.dir}" /> + <delete dir="${dist.dir}" /> + <delete dir="${doc.dir}" /> + </target> + + + <target name="compile"> + <delete dir="${build.dir}" /> + <mkdir dir="${build.dir}" /> + <javac srcdir="." destdir="${build.dir}" includeantruntime="false" /> + </target> + + + <target name="jar" depends="compile"> + <delete dir="${dist.dir}" /> + <mkdir dir="${dist.dir}" /> + <jar destfile="${dist.dir}/${jar.file}"> + <fileset dir="${build.dir}" /> + <manifest> + <attribute name="Main-Class" value="${class.main}" /> + </manifest> + </jar> + </target> + + + <target name="doc"> + <delete dir="${doc.dir}" /> + <mkdir dir="${doc.dir}" /> + <javadoc destdir="${doc.dir}" private="true" nohelp="true"> + <fileset dir="." /> + </javadoc> + </target> + +</project>