Skip to content

Commit

Permalink
Migrate to Java 8
Browse files Browse the repository at this point in the history
  • Loading branch information
jcgay committed Oct 7, 2018
1 parent a8ef16c commit c728c5e
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 127 deletions.
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
sudo: false
language: java
before_install:
- jdk_switcher use openjdk6
- sed -i "s|\[JDK6\]|$JAVA_HOME|g" etc/toolchains.xml
- jdk_switcher use oraclejdk8
install:
- mvn install -DskipTests=true -B -V -t etc/toolchains.xml
- mvn install -DskipTests=true -B -V
script:
- mvn verify -B -t etc/toolchains.xml
- mvn verify -B
after_success:
- "[[ ${TRAVIS_PULL_REQUEST} == 'false' ]] && [[ ${TRAVIS_TAG} == '' ]] && mvn deploy -DskipTests -t etc/toolchains.xml --settings etc/deploy-settings.xml"
- "[[ ${TRAVIS_PULL_REQUEST} == 'false' ]] && [[ ${TRAVIS_TAG} == '' ]] && mvn deploy -DskipTests --settings etc/deploy-settings.xml"
- mvn clean verify coveralls:report -Prun-coverage
env:
global:
Expand Down
15 changes: 0 additions & 15 deletions etc/toolchains.xml

This file was deleted.

15 changes: 12 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
</issueManagement>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<bintray.package>send-notification</bintray.package>
</properties>

Expand All @@ -50,6 +52,13 @@
</sourceDirectories>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
Expand All @@ -61,9 +70,9 @@

<profiles>
<profile>
<id>build-with-jdk-6</id>
<id>build-with-toolchains</id>
<activation>
<jdk>[1.7,)</jdk>
<jdk>!1.8</jdk>
</activation>
<build>
<plugins>
Expand All @@ -79,7 +88,7 @@
<configuration>
<toolchains>
<jdk>
<version>1.6</version>
<version>${maven.compiler.target}</version>
</jdk>
</toolchains>
</configuration>
Expand Down
16 changes: 2 additions & 14 deletions send-notification/src/main/java/fr/jcgay/notification/Icon.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.auto.value.AutoValue;
import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
Expand Down Expand Up @@ -42,14 +41,10 @@ public BufferedImage toImage() {
}

public byte[] toByteArray() {
InputStream is = null;
try {
is = content().openStream();
try (InputStream is = content().openStream()) {
return ByteStreams.toByteArray(is);
} catch (IOException e) {
throw new SendNotificationException("Error while reading status icon.", e);
} finally {
Closeables.closeQuietly(is);
}
}

Expand Down Expand Up @@ -77,19 +72,12 @@ public static Icon create(URL content, String id) {
}

private void write(File destination) throws IOException {
InputStream input = content().openStream();
FileOutputStream output = new FileOutputStream(destination);
try {
try (InputStream input = content().openStream(); FileOutputStream output = new FileOutputStream(destination)) {
byte[] buffer = new byte[1024 * 4];
int n;
while ((n = input.read(buffer)) != -1) {
output.write(buffer, 0, n);
}
} finally {
try {
output.close();
input.close();
} catch (IOException ignored) {}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.jcgay.notification;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand All @@ -9,7 +8,7 @@

public class MultipleSendNotificationException extends SendNotificationException {

private List<Exception> errors = new ArrayList<Exception>();
private List<Exception> errors;

public MultipleSendNotificationException(List<Exception> errors) {
super(messages(checkNotNull(errors)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public NotifierProvider(OperatingSystem os) {
public DiscoverableNotifier byName(ChosenNotifiers notifier, Properties properties, Application application) {

if (!notifier.secondary().isEmpty()) {
LinkedHashSet<DiscoverableNotifier> secondary = new LinkedHashSet<DiscoverableNotifier>(notifier.secondary().size());
LinkedHashSet<DiscoverableNotifier> secondary = new LinkedHashSet<>(notifier.secondary().size());
for (String secondaryNotifier : notifier.secondary()) {
secondary.add(byName(ChosenNotifiers.from(secondaryNotifier), properties, application));
}
Expand Down Expand Up @@ -130,15 +130,15 @@ public DiscoverableNotifier byName(ChosenNotifiers notifier, Properties properti

public Set<DiscoverableNotifier> available(Properties configuration, Application application) {
if (os.isMac()) {
Set<DiscoverableNotifier> macNotifiers = new LinkedHashSet<DiscoverableNotifier>();
Set<DiscoverableNotifier> macNotifiers = new LinkedHashSet<>();
macNotifiers.add(new GrowlNotifier(application, GrowlConfiguration.create(configuration), DEBUG));
macNotifiers.add(byName(NOTIFICATION_CENTER, configuration, application));
macNotifiers.add(byName(SYSTEM_TRAY, configuration, application));
return unmodifiableSet(macNotifiers);
}

if (os.isWindows()) {
Set<DiscoverableNotifier> winNotifiers = new LinkedHashSet<DiscoverableNotifier>();
Set<DiscoverableNotifier> winNotifiers = new LinkedHashSet<>();
winNotifiers.add(byName(SNARL, configuration, application));
winNotifiers.add(new GrowlNotifier(application, GrowlConfiguration.create(configuration), DEBUG));
winNotifiers.add(byName(TOASTER, configuration, application));
Expand All @@ -147,7 +147,7 @@ public Set<DiscoverableNotifier> available(Properties configuration, Application
return unmodifiableSet(winNotifiers);
}

Set<DiscoverableNotifier> linuxNotifiers = new LinkedHashSet<DiscoverableNotifier>();
Set<DiscoverableNotifier> linuxNotifiers = new LinkedHashSet<>();
linuxNotifiers.add(byName(KDIALOG, configuration, application));
linuxNotifiers.add(byName(NOTIFY_SEND, configuration, application));
linuxNotifiers.add(byName(SYSTEM_TRAY, configuration, application));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Set;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Arrays.asList;

@AutoValue
public abstract class ChosenNotifiers {
Expand All @@ -25,14 +26,11 @@ public static ChosenNotifiers from(String notifier) {
checkNotNull(notifier);

if (!notifier.contains(SEPARATOR)) {
return new AutoValue_ChosenNotifiers(notifier, Collections.<String>emptySet());
return new AutoValue_ChosenNotifiers(notifier, Collections.emptySet());
}

String[] notifiers = notifier.split(SEPARATOR);
LinkedHashSet<String> secondary = new LinkedHashSet<String>();
for (int i = 1; i < notifiers.length; i++) {
secondary.add(notifiers[i]);
}
Set<String> secondary = new LinkedHashSet<>(asList(notifiers).subList(1, notifiers.length));
return new AutoValue_ChosenNotifiers(notifiers[0], secondary);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AdditionalNotifier(DiscoverableNotifier primary, Set<DiscoverableNotifier

@Override
public Notifier init() {
List<Exception> errors = new ArrayList<Exception>();
List<Exception> errors = new ArrayList<>();

safeInit(primary, errors);

Expand All @@ -55,7 +55,7 @@ public boolean tryInit() {

@Override
public void send(Notification notification) {
List<Exception> errors = new ArrayList<Exception>();
List<Exception> errors = new ArrayList<>();

safeSend(primary, notification, errors);

Expand All @@ -70,7 +70,7 @@ public void send(Notification notification) {

@Override
public void close() {
List<Exception> errors = new ArrayList<Exception>();
List<Exception> errors = new ArrayList<>();

safeClose(primary, errors);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fr.jcgay.notification.notifier.anybar;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.Closeables;
import fr.jcgay.notification.Icon;
import fr.jcgay.notification.IconFileWriter;
import fr.jcgay.notification.SendNotificationException;
Expand All @@ -11,9 +10,6 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static com.google.common.io.Closeables.closeQuietly;

public class AnyBarIconWriter implements IconFileWriter {

Expand All @@ -36,19 +32,10 @@ public void write(Icon icon) {
if (!resizedIcon.exists()) {
new File(destination).mkdirs();

InputStream input = null;
OutputStream output = null;
try {
input = icon.content().openStream();
output = new FileOutputStream(resizedIcon);
try (InputStream input = icon.content().openStream(); FileOutputStream output = new FileOutputStream(resizedIcon)) {
Thumbnailator.createThumbnail(input, output, dimension.width, dimension.height);
} catch (IOException e) {
throw new SendNotificationException("Can't write notification icon: " + resizedIcon.getPath(), e);
} finally {
closeQuietly(input);
try {
Closeables.close(output, true);
} catch (IOException ignored) {}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;

import static com.google.common.io.Closeables.closeQuietly;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.slf4j.LoggerFactory.getLogger;

Expand All @@ -39,31 +37,28 @@ public void exec(final String[] command) {
LOGGER.debug("Will execute command line: {} (waiting at most: {} milliseconds)", logCommand(command), timeout);
}

Future<Integer> task = executor.submit(new Callable<Integer>() {
@Override
public Integer call() {
try {
Process execution = new ProcessBuilder(command)
.redirectErrorStream(true)
.start();

int returnCode = execution.waitFor();
if (returnCode != 0) {
String message = "Command <[" + logCommand(command) + "]> returns code: " + returnCode + ".\n" + asString(execution.getInputStream());
LOGGER.debug(message);
throw new SendNotificationException(message);
}

LOGGER.debug("Command <[{}]> ends successfully.", logCommand(command));

return returnCode;

} catch (IOException e) {
throw Throwables.propagate(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw Throwables.propagate(e);
Future<Integer> task = executor.submit(() -> {
try {
Process execution = new ProcessBuilder(command)
.redirectErrorStream(true)
.start();

int returnCode = execution.waitFor();
if (returnCode != 0) {
String message = "Command <[" + logCommand(command) + "]> returns code: " + returnCode + ".\n" + asString(execution.getInputStream());
LOGGER.debug(message);
throw new SendNotificationException(message);
}

LOGGER.debug("Command <[{}]> ends successfully.", logCommand(command));

return returnCode;

} catch (IOException e) {
throw Throwables.propagate(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw Throwables.propagate(e);
}
});

Expand Down Expand Up @@ -95,17 +90,13 @@ private static String logCommand(String[] command) {
}

private static String asString(InputStream inputStream) throws IOException {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(inputStream));
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
StringBuilder output = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
return output.toString();
} finally {
closeQuietly(reader);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class KdialogNotifier implements DiscoverableNotifier {
private final Executor executor;

public KdialogNotifier(Application application, KdialogConfiguration configuration, Executor executor) {
LOGGER.debug("Configuring notifu for application {}: {}.", application, configuration);
LOGGER.debug("Configuring Kdialog for application {}: {}.", application, configuration);
this.application = application;
this.configuration = configuration;
this.executor = executor;
Expand All @@ -37,7 +37,7 @@ public Notifier init() {

@Override
public void send(Notification notification) {
List<String> commands = new ArrayList<String>();
List<String> commands = new ArrayList<>();
commands.add(configuration.bin());
commands.add("--passivepopup");
commands.add(notification.message());
Expand All @@ -50,7 +50,7 @@ public void send(Notification notification) {
commands.add(notification.icon().asPath());

try {
executor.exec(commands.toArray(new String[commands.size()]));
executor.exec(commands.toArray(new String[0]));
} catch (RuntimeException e) {
throw new KdialogException("Error while sending notification with Kdialog.", e);
}
Expand All @@ -68,11 +68,11 @@ public boolean isPersistent() {

@Override
public boolean tryInit() {
List<String> commands = new ArrayList<String>();
List<String> commands = new ArrayList<>();
commands.add(configuration.bin());
commands.add("-v");

return executor.tryExec(commands.toArray(new String[commands.size()]));
return executor.tryExec(commands.toArray(new String[0]));
}

@Override
Expand Down

0 comments on commit c728c5e

Please sign in to comment.