Skip to content

Commit

Permalink
even better naming
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <github@klug.nrw>
  • Loading branch information
J-N-K committed Feb 5, 2023
1 parent f4b5f49 commit bdc78e1
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 88 deletions.
Expand Up @@ -17,21 +17,22 @@
import org.openhab.core.types.Command;

/**
* The {@link ChannelValueHandler} defines the interface for converting received {@link ChannelValueConverterContent}
* The {@link ChannelHandler} defines the interface for converting received {@link ChannelHandlerContent}
* to {@link org.openhab.core.types.State}s for posting updates to {@link org.openhab.core.thing.Channel}s and
* {@link Command}s to values for sending
*
* @author Jan N. Klug - Initial contribution
*/
@NonNullByDefault
public interface ChannelValueHandler {
public interface ChannelHandler {

/**
* called to process a given content for this channel
*
* @param content raw content to process (<code>null</code> results in {@link org.openhab.core.types.UnDefType#UNDEF})
* @param content raw content to process (<code>null</code> results in
* {@link org.openhab.core.types.UnDefType#UNDEF})
*/
void process(@Nullable ChannelValueConverterContent content);
void process(@Nullable ChannelHandlerContent content);

/**
* called to send a command to this channel
Expand Down
Expand Up @@ -19,17 +19,17 @@
import org.eclipse.jdt.annotation.Nullable;

/**
* The {@link ChannelValueConverterContent} defines the pre-processed response
* The {@link ChannelHandlerContent} defines the pre-processed response
*
* @author Jan N. Klug - Initial contribution
*/
@NonNullByDefault
public class ChannelValueConverterContent {
public class ChannelHandlerContent {
private final byte[] rawContent;
private final Charset encoding;
private final @Nullable String mediaType;

public ChannelValueConverterContent(byte[] rawContent, String encoding, @Nullable String mediaType) {
public ChannelHandlerContent(byte[] rawContent, String encoding, @Nullable String mediaType) {
this.rawContent = rawContent;
this.mediaType = mediaType;

Expand Down
Expand Up @@ -26,13 +26,13 @@
import org.openhab.core.library.types.RewindFastforwardType;
import org.openhab.core.library.types.StopMoveType;
import org.openhab.core.library.types.UpDownType;
import org.openhab.core.thing.binding.generic.converter.ColorChannelValueHandler;
import org.openhab.core.thing.binding.generic.converter.ColorChannelHandler;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;

/**
* The {@link ChannelValueConverterConfig} is a base class for the channel configuration of things
* using the {@link ChannelValueHandler}s
* using the {@link ChannelHandler}s
*
* @author Jan N. Klug - Initial contribution
*/
Expand All @@ -57,7 +57,7 @@ public class ChannelValueConverterConfig {
public @Nullable String decreaseValue;

// color
public ColorChannelValueHandler.ColorMode colorMode = ColorChannelValueHandler.ColorMode.RGB;
public ColorChannelHandler.ColorMode colorMode = ColorChannelHandler.ColorMode.RGB;

// contact
public @Nullable String openValue;
Expand Down
Expand Up @@ -17,22 +17,22 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.thing.binding.generic.ChannelHandler;
import org.openhab.core.thing.binding.generic.ChannelHandlerContent;
import org.openhab.core.thing.binding.generic.ChannelMode;
import org.openhab.core.thing.binding.generic.ChannelTransformation;
import org.openhab.core.thing.binding.generic.ChannelValueHandler;
import org.openhab.core.thing.binding.generic.ChannelValueConverterConfig;
import org.openhab.core.thing.binding.generic.ChannelValueConverterContent;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;

/**
* The {@link AbstractTransformingChannelValueHandler} is a base class for an item converter with transformations
* The {@link AbstractTransformingChannelHandler} is a base class for an item converter with transformations
*
* @author Jan N. Klug - Initial contribution
*/
@NonNullByDefault
public abstract class AbstractTransformingChannelValueHandler implements ChannelValueHandler {
public abstract class AbstractTransformingChannelHandler implements ChannelHandler {
private final Consumer<State> updateState;
private final Consumer<Command> postCommand;
private final @Nullable Consumer<String> sendValue;
Expand All @@ -41,7 +41,7 @@ public abstract class AbstractTransformingChannelValueHandler implements Channel

protected final ChannelValueConverterConfig channelConfig;

public AbstractTransformingChannelValueHandler(Consumer<State> updateState, Consumer<Command> postCommand,
public AbstractTransformingChannelHandler(Consumer<State> updateState, Consumer<Command> postCommand,
@Nullable Consumer<String> sendValue, ChannelTransformation stateTransformations,
ChannelTransformation commandTransformations, ChannelValueConverterConfig channelConfig) {
this.updateState = updateState;
Expand All @@ -53,7 +53,7 @@ public AbstractTransformingChannelValueHandler(Consumer<State> updateState, Cons
}

@Override
public void process(@Nullable ChannelValueConverterContent content) {
public void process(@Nullable ChannelHandlerContent content) {
if (content == null) {
updateState.accept(UnDefType.UNDEF);
return;
Expand Down Expand Up @@ -108,7 +108,7 @@ public void send(Command command) {

@FunctionalInterface
public interface Factory {
ChannelValueHandler create(Consumer<State> updateState, Consumer<Command> postCommand,
ChannelHandler create(Consumer<State> updateState, Consumer<Command> postCommand,
@Nullable Consumer<String> sendHttpValue, ChannelTransformation stateTransformations,
ChannelTransformation commandTransformations, ChannelValueConverterConfig channelConfig);
}
Expand Down
Expand Up @@ -29,20 +29,20 @@
import org.openhab.core.types.UnDefType;

/**
* The {@link ColorChannelValueHandler} implements {@link org.openhab.core.library.items.ColorItem} conversions
* The {@link ColorChannelHandler} implements {@link org.openhab.core.library.items.ColorItem} conversions
*
* @author Jan N. Klug - Initial contribution
*/

@NonNullByDefault
public class ColorChannelValueHandler extends AbstractTransformingChannelValueHandler {
public class ColorChannelHandler extends AbstractTransformingChannelHandler {
private static final BigDecimal BYTE_FACTOR = BigDecimal.valueOf(2.55);
private static final BigDecimal HUNDRED = BigDecimal.valueOf(100);
private static final Pattern TRIPLE_MATCHER = Pattern.compile("(?<r>\\d+),(?<g>\\d+),(?<b>\\d+)");

private State state = UnDefType.UNDEF;

public ColorChannelValueHandler(Consumer<State> updateState, Consumer<Command> postCommand,
public ColorChannelHandler(Consumer<State> updateState, Consumer<Command> postCommand,
@Nullable Consumer<String> sendValue, ChannelTransformation stateTransformations,
ChannelTransformation commandTransformations, ChannelValueConverterConfig channelConfig) {
super(updateState, postCommand, sendValue, stateTransformations, commandTransformations, channelConfig);
Expand Down
Expand Up @@ -26,18 +26,18 @@
import org.openhab.core.types.UnDefType;

/**
* The {@link DimmerChannelValueHandler} implements {@link org.openhab.core.library.items.DimmerItem} conversions
* The {@link DimmerChannelHandler} implements {@link org.openhab.core.library.items.DimmerItem} conversions
*
* @author Jan N. Klug - Initial contribution
*/

@NonNullByDefault
public class DimmerChannelValueHandler extends AbstractTransformingChannelValueHandler {
public class DimmerChannelHandler extends AbstractTransformingChannelHandler {
private static final BigDecimal HUNDRED = BigDecimal.valueOf(100);

private State state = UnDefType.UNDEF;

public DimmerChannelValueHandler(Consumer<State> updateState, Consumer<Command> postCommand,
public DimmerChannelHandler(Consumer<State> updateState, Consumer<Command> postCommand,
@Nullable Consumer<String> sendValue, ChannelTransformation stateTransformations,
ChannelTransformation commandTransformations, ChannelValueConverterConfig channelConfig) {
super(updateState, postCommand, sendValue, stateTransformations, commandTransformations, channelConfig);
Expand Down
Expand Up @@ -25,15 +25,15 @@
import org.openhab.core.types.UnDefType;

/**
* The {@link FixedValueMappingChannelValueHandler} implements mapping conversions for different item-types
* The {@link FixedValueMappingChannelHandler} implements mapping conversions for different item-types
*
* @author Jan N. Klug - Initial contribution
*/

@NonNullByDefault
public class FixedValueMappingChannelValueHandler extends AbstractTransformingChannelValueHandler {
public class FixedValueMappingChannelHandler extends AbstractTransformingChannelHandler {

public FixedValueMappingChannelValueHandler(Consumer<State> updateState, Consumer<Command> postCommand,
public FixedValueMappingChannelHandler(Consumer<State> updateState, Consumer<Command> postCommand,
@Nullable Consumer<String> sendValue, ChannelTransformation stateTransformations,
ChannelTransformation commandTransformations, ChannelValueConverterConfig channelConfig) {
super(updateState, postCommand, sendValue, stateTransformations, commandTransformations, channelConfig);
Expand Down
Expand Up @@ -25,15 +25,15 @@
import org.openhab.core.types.UnDefType;

/**
* The {@link GenericChannelValueHandler} implements simple conversions for different item types
* The {@link GenericChannelHandler} implements simple conversions for different item types
*
* @author Jan N. Klug - Initial contribution
*/
@NonNullByDefault
public class GenericChannelValueHandler extends AbstractTransformingChannelValueHandler {
public class GenericChannelHandler extends AbstractTransformingChannelHandler {
private final Function<String, State> toState;

public GenericChannelValueHandler(Function<String, State> toState, Consumer<State> updateState,
public GenericChannelHandler(Function<String, State> toState, Consumer<State> updateState,
Consumer<Command> postCommand, @Nullable Consumer<String> sendValue,
ChannelTransformation stateTransformations, ChannelTransformation commandTransformations,
ChannelValueConverterConfig channelConfig) {
Expand Down
Expand Up @@ -17,28 +17,28 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.library.types.RawType;
import org.openhab.core.thing.binding.generic.ChannelValueHandler;
import org.openhab.core.thing.binding.generic.ChannelValueConverterContent;
import org.openhab.core.thing.binding.generic.ChannelHandler;
import org.openhab.core.thing.binding.generic.ChannelHandlerContent;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;

/**
* The {@link ImageChannelValueHandler} implements {@link org.openhab.core.library.items.ImageItem} conversions
* The {@link ImageChannelHandler} implements {@link org.openhab.core.library.items.ImageItem} conversions
*
* @author Jan N. Klug - Initial contribution
*/

@NonNullByDefault
public class ImageChannelValueHandler implements ChannelValueHandler {
public class ImageChannelHandler implements ChannelHandler {
private final Consumer<State> updateState;

public ImageChannelValueHandler(Consumer<State> updateState) {
public ImageChannelHandler(Consumer<State> updateState) {
this.updateState = updateState;
}

@Override
public void process(@Nullable ChannelValueConverterContent content) {
public void process(@Nullable ChannelHandlerContent content) {
if (content == null) {
updateState.accept(UnDefType.UNDEF);
return;
Expand Down
Expand Up @@ -28,14 +28,14 @@
import org.openhab.core.types.UnDefType;

/**
* The {@link NumberChannelValueHandler} implements {@link org.openhab.core.library.items.NumberItem} conversions
* The {@link NumberChannelHandler} implements {@link org.openhab.core.library.items.NumberItem} conversions
*
* @author Jan N. Klug - Initial contribution
*/
@NonNullByDefault
public class NumberChannelValueHandler extends AbstractTransformingChannelValueHandler {
public class NumberChannelHandler extends AbstractTransformingChannelHandler {

public NumberChannelValueHandler(Consumer<State> updateState, Consumer<Command> postCommand,
public NumberChannelHandler(Consumer<State> updateState, Consumer<Command> postCommand,
@Nullable Consumer<String> sendValue, ChannelTransformation stateTransformations,
ChannelTransformation commandTransformations, ChannelValueConverterConfig channelConfig) {
super(updateState, postCommand, sendValue, stateTransformations, commandTransformations, channelConfig);
Expand Down
Expand Up @@ -26,17 +26,17 @@
import org.openhab.core.types.State;

/**
* The {@link PlayerChannelValueHandler} implements {@link org.openhab.core.library.items.RollershutterItem}
* The {@link PlayerChannelHandler} implements {@link org.openhab.core.library.items.RollershutterItem}
* conversions
*
* @author Jan N. Klug - Initial contribution
*/

@NonNullByDefault
public class PlayerChannelValueHandler extends AbstractTransformingChannelValueHandler {
public class PlayerChannelHandler extends AbstractTransformingChannelHandler {
private @Nullable String lastCommand; // store last command to prevent duplicate commands

public PlayerChannelValueHandler(Consumer<State> updateState, Consumer<Command> postCommand,
public PlayerChannelHandler(Consumer<State> updateState, Consumer<Command> postCommand,
@Nullable Consumer<String> sendValue, ChannelTransformation stateTransformations,
ChannelTransformation commandTransformations, ChannelValueConverterConfig channelConfig) {
super(updateState, postCommand, sendValue, stateTransformations, commandTransformations, channelConfig);
Expand Down
Expand Up @@ -28,16 +28,16 @@
import org.openhab.core.types.UnDefType;

/**
* The {@link RollershutterChannelValueHandler} implements {@link org.openhab.core.library.items.RollershutterItem}
* The {@link RollershutterChannelHandler} implements {@link org.openhab.core.library.items.RollershutterItem}
* conversions
*
* @author Jan N. Klug - Initial contribution
*/

@NonNullByDefault
public class RollershutterChannelValueHandler extends AbstractTransformingChannelValueHandler {
public class RollershutterChannelHandler extends AbstractTransformingChannelHandler {

public RollershutterChannelValueHandler(Consumer<State> updateState, Consumer<Command> postCommand,
public RollershutterChannelHandler(Consumer<State> updateState, Consumer<Command> postCommand,
@Nullable Consumer<String> sendValue, ChannelTransformation stateTransformations,
ChannelTransformation commandTransformations, ChannelValueConverterConfig channelConfig) {
super(updateState, postCommand, sendValue, stateTransformations, commandTransformations, channelConfig);
Expand Down
Expand Up @@ -31,16 +31,16 @@
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.binding.generic.ChannelHandlerContent;
import org.openhab.core.thing.binding.generic.ChannelTransformation;
import org.openhab.core.thing.binding.generic.ChannelValueConverterConfig;
import org.openhab.core.thing.binding.generic.ChannelValueConverterContent;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;

/**
* The {@link AbstractTransformingItemConverterTest} is a test class for the
* {@link AbstractTransformingChannelValueHandler}
* {@link AbstractTransformingChannelHandler}
*
* @author Jan N. Klug - Initial contribution
*/
Expand Down Expand Up @@ -76,9 +76,9 @@ public void close() throws Exception {

@Test
public void undefOnNullContentTest() {
TestChannelValueHandler realConverter = new TestChannelValueHandler(updateState, postCommand, sendHttpValue,
TestChannelHandler realConverter = new TestChannelHandler(updateState, postCommand, sendHttpValue,
stateChannelTransformation, commandChannelTransformation, false);
TestChannelValueHandler converter = spy(realConverter);
TestChannelHandler converter = spy(realConverter);

converter.process(null);
// make sure UNDEF is send as state update
Expand All @@ -94,10 +94,10 @@ public void undefOnNullContentTest() {

@Test
public void commandIsPostedAsCommand() {
TestChannelValueHandler converter = new TestChannelValueHandler(updateState, postCommand, sendHttpValue,
TestChannelHandler converter = new TestChannelHandler(updateState, postCommand, sendHttpValue,
stateChannelTransformation, commandChannelTransformation, true);

converter.process(new ChannelValueConverterContent("TEST".getBytes(StandardCharsets.UTF_8), "", null));
converter.process(new ChannelHandlerContent("TEST".getBytes(StandardCharsets.UTF_8), "", null));

// check state transformation is applied
verify(stateChannelTransformation).apply(any());
Expand All @@ -111,10 +111,10 @@ public void commandIsPostedAsCommand() {

@Test
public void updateIsPostedAsUpdate() {
TestChannelValueHandler converter = new TestChannelValueHandler(updateState, postCommand, sendHttpValue,
TestChannelHandler converter = new TestChannelHandler(updateState, postCommand, sendHttpValue,
stateChannelTransformation, commandChannelTransformation, false);

converter.process(new ChannelValueConverterContent("TEST".getBytes(StandardCharsets.UTF_8), "", null));
converter.process(new ChannelHandlerContent("TEST".getBytes(StandardCharsets.UTF_8), "", null));

// check state transformation is applied
verify(stateChannelTransformation).apply(any());
Expand All @@ -128,7 +128,7 @@ public void updateIsPostedAsUpdate() {

@Test
public void sendCommandSendsCommand() {
TestChannelValueHandler converter = new TestChannelValueHandler(updateState, postCommand, sendHttpValue,
TestChannelHandler converter = new TestChannelHandler(updateState, postCommand, sendHttpValue,
stateChannelTransformation, commandChannelTransformation, false);

converter.send(new StringType("TEST"));
Expand All @@ -143,10 +143,10 @@ public void sendCommandSendsCommand() {
verify(sendHttpValue, only()).accept("TEST");
}

private static class TestChannelValueHandler extends AbstractTransformingChannelValueHandler {
private static class TestChannelHandler extends AbstractTransformingChannelHandler {
private boolean hasCommand;

public TestChannelValueHandler(Consumer<State> updateState, Consumer<Command> postCommand,
public TestChannelHandler(Consumer<State> updateState, Consumer<Command> postCommand,
@Nullable Consumer<String> sendValue, ChannelTransformation stateChannelTransformation,
ChannelTransformation commandChannelTransformation, boolean hasCommand) {
super(updateState, postCommand, sendValue, stateChannelTransformation, commandChannelTransformation,
Expand Down

0 comments on commit bdc78e1

Please sign in to comment.