Skip to content

Commit

Permalink
Merge bee5999 into 49c3f66
Browse files Browse the repository at this point in the history
  • Loading branch information
sjamesr committed Jan 1, 2020
2 parents 49c3f66 + bee5999 commit 209e03c
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 83 deletions.
1 change: 0 additions & 1 deletion src/main/java/au/com/southsky/jfreesane/OptionGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.List;

import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

Expand Down
43 changes: 43 additions & 0 deletions src/main/java/au/com/southsky/jfreesane/Preconditions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package au.com.southsky.jfreesane;

final class Preconditions {
private Preconditions() {}

static void checkState(boolean state, String message, Object... args) {
if (!state) {
throw new IllegalStateException(String.format(message, args));
}
}

static void checkState(boolean state, String message) {
if (!state) {
throw new IllegalStateException(message);
}
}

static void checkState(boolean state) {
if (!state) {
throw new IllegalStateException();
}
}

static void checkArgument(boolean arg) {
if (!arg) {
throw new IllegalArgumentException();
}
}

static void checkArgument(boolean arg, String message, Object... args) {
if (!arg) {
throw new IllegalArgumentException(String.format(message, args));
}
}

static <T> T checkNotNull(T obj) {
if (obj == null) {
throw new NullPointerException();
}

return obj;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package au.com.southsky.jfreesane;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.HashBasedTable;
Expand All @@ -14,6 +13,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
Expand Down Expand Up @@ -48,7 +48,7 @@ public SaneClientAuthentication(final String path) {
new CharSource() {
@Override
public Reader openStream() throws IOException {
return new InputStreamReader(new FileInputStream(path), Charsets.US_ASCII);
return new InputStreamReader(new FileInputStream(path), StandardCharsets.US_ASCII);
}
});
}
Expand Down
15 changes: 4 additions & 11 deletions src/main/java/au/com/southsky/jfreesane/SaneDevice.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package au.com.southsky.jfreesane;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.awt.image.BufferedImage;
import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Represents a SANE device within a session. SANE devices are obtained from a {@link SaneSession}.
Expand Down Expand Up @@ -168,14 +166,9 @@ public List<SaneOption> listOptions() throws IOException {
if (optionTitleMap == null) {
groups.clear();
optionTitleMap =
Maps.uniqueIndex(
SaneOption.optionsFor(this),
new Function<SaneOption, String>() {
@Override
public String apply(SaneOption input) {
return input.getName();
}
});
SaneOption.optionsFor(this)
.stream()
.collect(Collectors.toMap(SaneOption::getName, option -> option));
}

// Maps.uniqueIndex guarantees the order of optionTitleMap.values()
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/au/com/southsky/jfreesane/SaneImage.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package au.com.southsky.jfreesane;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
Expand Down Expand Up @@ -45,13 +43,7 @@ private SaneImage(
this.frames =
Ordering.explicit(
FrameType.RED, FrameType.GREEN, FrameType.BLUE, FrameType.RGB, FrameType.GRAY)
.onResultOf(
new Function<Frame, FrameType>() {
@Override
public FrameType apply(Frame input) {
return input.getType();
}
})
.onResultOf(Frame::getType)
.immutableSortedCopy(frames);
this.depthPerPixel = depthPerPixel;
this.width = width;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/au/com/southsky/jfreesane/SaneInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import au.com.southsky.jfreesane.SaneOption.OptionUnits;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.io.ByteStreams;
Expand Down Expand Up @@ -122,7 +122,7 @@ public String readString() throws IOException {
}

// skip the null terminator
return new String(input, 0, input.length - 1, Charsets.ISO_8859_1);
return new String(input, 0, input.length - 1, StandardCharsets.ISO_8859_1);
}

public SaneParameters readSaneParameters() throws IOException {
Expand Down
44 changes: 25 additions & 19 deletions src/main/java/au/com/southsky/jfreesane/SaneOption.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package au.com.southsky.jfreesane;

import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.io.ByteStreams;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
* This class represents a SANE device option. An option may be active or inactive (see
Expand Down Expand Up @@ -296,11 +297,19 @@ public List<SaneWord> getWordConstraints() {
}

public List<Integer> getIntegerValueListConstraint() {
return Lists.transform(descriptor.getWordConstraints(), SaneWord.TO_INTEGER_FUNCTION);
return descriptor
.getWordConstraints()
.stream()
.map(SaneWord::integerValue)
.collect(Collectors.toList());
}

public List<Double> getFixedValueListConstraint() {
return Lists.transform(descriptor.getWordConstraints(), SaneWord.TO_FIXED_FUNCTION);
return descriptor
.getWordConstraints()
.stream()
.map(SaneWord::fixedPrecisionValue)
.collect(Collectors.toList());
}

@Override
Expand Down Expand Up @@ -382,7 +391,7 @@ public List<Integer> getIntegerArrayValue() throws IOException, SaneException {
* @throws IOException if a problem occurs reading the value from the SANE backend
*/
public String getStringValue() throws IOException, SaneException {
return getStringValue(Charsets.ISO_8859_1);
return getStringValue(StandardCharsets.ISO_8859_1);
}

public String getStringValue(Charset encoding) throws IOException, SaneException {
Expand Down Expand Up @@ -413,7 +422,7 @@ public List<Double> getFixedArrayValue() throws IOException, SaneException {
ControlOptionResult result = readOption();
Preconditions.checkState(result.getType() == OptionValueType.FIXED);

List<Double> values = Lists.newArrayList();
List<Double> values = new ArrayList<>();
for (int i = 0; i < result.getValueSize(); i += SaneWord.SIZE_IN_BYTES) {
values.add(SaneWord.fromBytes(result.getValue(), i).fixedPrecisionValue());
}
Expand Down Expand Up @@ -487,7 +496,7 @@ public void setButtonValue() throws IOException, SaneException {
*/
public double setFixedValue(double value) throws IOException, SaneException {
Preconditions.checkArgument(
value >= -32768 && value <= 32767.9999, "value " + value + " is out of range");
value >= -32768 && value <= 32767.9999, "value %d is out of range", value);
SaneWord wordValue = SaneWord.forFixedPrecision(value);
ControlOptionResult result = writeOption(wordValue);
Preconditions.checkState(
Expand All @@ -497,22 +506,19 @@ public double setFixedValue(double value) throws IOException, SaneException {
return SaneWord.fromBytes(result.getValue()).fixedPrecisionValue();
}

private static SaneWord fixedValueToWord(double value) {
Preconditions.checkArgument(
value >= -32768 && value <= 32767.9999, "value %f is out of range", value);
return SaneWord.forFixedPrecision(value);
}

/**
* Sets the value of the current option to the supplied list of fixed-precision values. Option
* value must be of fixed-precision type and {@link #getValueCount} must be more than 1.
*/
public List<Double> setFixedValue(List<Double> value) throws IOException, SaneException {
List<SaneWord> wordValues =
Lists.transform(
value,
new Function<Double, SaneWord>() {
@Override
public SaneWord apply(Double input) {
Preconditions.checkArgument(
input >= -32768 && input <= 32767.9999, "value " + input + " is out of range");
return SaneWord.forFixedPrecision(input);
}
});
value.stream().map(SaneOption::fixedValueToWord).collect(Collectors.toList());

ControlOptionResult result = writeWordListOption(wordValues);

Expand Down Expand Up @@ -550,7 +556,7 @@ public String setStringValue(String newValue) throws IOException, SaneException

// TODO(sjr): maybe this should go somewhere common?
String optionValueFromServer =
new String(result.getValue(), 0, result.getValueSize() - 1, Charsets.ISO_8859_1);
new String(result.getValue(), 0, result.getValueSize() - 1, StandardCharsets.ISO_8859_1);

Preconditions.checkState(
result.getInfo().contains(OptionWriteInfo.INEXACT) ^ newValue.equals(optionValueFromServer),
Expand Down
1 change: 0 additions & 1 deletion src/main/java/au/com/southsky/jfreesane/SaneSession.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package au.com.southsky.jfreesane;

import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
Expand Down
34 changes: 2 additions & 32 deletions src/main/java/au/com/southsky/jfreesane/SaneWord.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package au.com.southsky.jfreesane;

import com.google.common.io.ByteStreams;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
Expand All @@ -8,10 +10,6 @@
import java.io.InputStream;
import java.util.Arrays;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.io.ByteStreams;

/**
* Represents a SANE word type. JFreeSane chooses to represent the SANE word type as an array of
* {@link #SIZE_IN_BYTES} bytes.
Expand All @@ -31,34 +29,6 @@ public final class SaneWord {

private static final int PRECISION = 1 << 16;

/**
* A function that, when applied to a {@link SaneWord} instance, returns the integer value of that
* SANE word.
*
* @see SaneWord#integerValue
*/
public static final Function<SaneWord, Integer> TO_INTEGER_FUNCTION =
new Function<SaneWord, Integer>() {
@Override
public Integer apply(SaneWord word) {
return word.integerValue();
}
};

/**
* A function that, when applied to a {@link SaneWord} instance, returns the SANE fixed precision
* value of that SANE word.
*
* @see SaneWord#fixedPrecisionValue
*/
public static final Function<SaneWord, Double> TO_FIXED_FUNCTION =
new Function<SaneWord, Double>() {
@Override
public Double apply(SaneWord word) {
return word.fixedPrecisionValue();
}
};

private final byte[] value;

private SaneWord(byte[] value) {
Expand Down
14 changes: 8 additions & 6 deletions src/test/java/au/com/southsky/jfreesane/SaneSessionTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package au.com.southsky.jfreesane;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.io.Closeables;
Expand All @@ -22,6 +21,7 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -158,7 +158,7 @@ public void getOptionValueSucceeds() throws Exception {
&& option.isActive()) {
System.out.print("=" + option.getIntegerValue());
} else if (option.getType() == OptionValueType.STRING) {
System.out.print("=" + option.getStringValue(Charsets.US_ASCII));
System.out.print("=" + option.getStringValue(StandardCharsets.US_ASCII));
}
}

Expand Down Expand Up @@ -234,10 +234,12 @@ public void readsAndSetsStringsCorrectly() throws Exception {

try (SaneDevice device = session.getDevice("test")) {
device.open();
assertThat(device.getOption("mode").getStringValue(Charsets.US_ASCII)).matches("Gray|Color");
assertThat(device.getOption("mode").getStringValue(StandardCharsets.US_ASCII))
.matches("Gray|Color");
assertThat(device.getOption("mode").setStringValue("Gray")).isEqualTo("Gray");
assertThat(device.getOption("mode").getStringValue(Charsets.US_ASCII)).isEqualTo("Gray");
assertThat(device.getOption("read-return-value").getStringValue(Charsets.US_ASCII))
assertThat(device.getOption("mode").getStringValue(StandardCharsets.US_ASCII))
.isEqualTo("Gray");
assertThat(device.getOption("read-return-value").getStringValue(StandardCharsets.US_ASCII))
.isEqualTo("Default");
}
}
Expand Down Expand Up @@ -497,7 +499,7 @@ public boolean canAuthenticate(String resource) {
@Test
public void passwordAuthenticationFromLocalFileSpecified() throws Exception {
File passwordFile = tempFolder.newFile("sane.pass");
Files.asCharSink(passwordFile, Charsets.ISO_8859_1).write("testuser:goodpass:test");
Files.asCharSink(passwordFile, StandardCharsets.ISO_8859_1).write("testuser:goodpass:test");
session.setPasswordProvider(
SanePasswordProvider.usingSanePassFile(passwordFile.getAbsolutePath()));
SaneDevice device = session.getDevice("test");
Expand Down

0 comments on commit 209e03c

Please sign in to comment.