Skip to content

Commit

Permalink
fixed remaining implementations and added tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Feb 2, 2018
1 parent 7805d0c commit 8e0e8e6
Show file tree
Hide file tree
Showing 13 changed files with 396 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,26 @@ public class SampleSize extends ByteValue {
assertLength(NUM_BYTES);
}

public static SampleSize of(int size) {
return new SampleSize(ByteBuffer.allocate(NUM_BYTES).putInt(size).array());
public static SampleSize of(long sampleSize) {
checkUnsignedBounds(sampleSize, NUM_BYTES);
return new SampleSize(ByteBuffer.allocate(NUM_BYTES)
.put((byte) (sampleSize >> 24 & 0xff))
.put((byte) (sampleSize >> 16 & 0xff))
.put((byte) (sampleSize >> 8 & 0xff))
.put((byte) (sampleSize & 0xff))
.array());
}

public static SampleSize of(String sampleSize) {
return of(Long.parseLong(sampleSize));
}

public static SampleSize of(byte... values) {
return new SampleSize(values);
}

@Override
public String toString() {
return "" + (getBytes()[0] << 24 | getBytes()[1] << 16 | getBytes()[2] << 8 | getBytes()[3]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,26 @@ public class Samples extends ByteValue {
assertLength(NUM_BYTES);
}

public static Samples of(int numberOfSamples) {
return new Samples(ByteBuffer.allocate(NUM_BYTES).putInt(numberOfSamples).array());
public static Samples of(long numberOfSamples) {
checkUnsignedBounds(numberOfSamples, NUM_BYTES);
return new Samples(ByteBuffer.allocate(NUM_BYTES)
.put((byte) (numberOfSamples >> 24 & 0xff))
.put((byte) (numberOfSamples >> 16 & 0xff))
.put((byte) (numberOfSamples >> 8 & 0xff))
.put((byte) (numberOfSamples & 0xff))
.array());
}

public static Samples of(String numberOfSamples) {
return of(Long.parseLong(numberOfSamples));
}

public static Samples of(byte... values) {
return new Samples(values);
}

@Override
public String toString() {
return "" + (getBytes()[0] << 24 | getBytes()[1] << 16 | getBytes()[2] << 8 | getBytes()[3]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,26 @@ public class WriteLength extends ByteValue {
assertLength(NUM_BYTES);
}

public static WriteLength of(long length) {
checkUnsignedBounds(length, NUM_BYTES);
return new WriteLength(ByteBuffer.allocate(NUM_BYTES)
.put((byte) (length >> 24 & 0xff))
.put((byte) (length >> 16 & 0xff))
.put((byte) (length >> 8 & 0xff))
.put((byte) (length & 0xff))
.array());
}

public static WriteLength of(String writeLength) {
return of(Long.parseLong(writeLength));
}

public static WriteLength of(byte... values) {
return new WriteLength(values);
}

public static WriteLength of(int length) {
return new WriteLength(ByteBuffer.allocate(NUM_BYTES).putInt(length).array());
@Override
public String toString() {
return "" + (getBytes()[0] << 24 | getBytes()[1] << 16 | getBytes()[2] << 8 | getBytes()[3]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.apache.plc4x.java.ads.api.util.ByteReadable;
import org.apache.plc4x.java.ads.api.util.ByteValue;

import java.nio.ByteBuffer;

Expand All @@ -38,14 +39,20 @@ public enum Command implements ByteReadable {
/**
* Other commands are not defined or are used internally. Therefore the Command Id is only allowed to contain the above enumerated values!
*/
UNKNOWN(0xffff);
UNKNOWN(0xffff_ffff);

public static final int NUM_BYTES = 4;

final byte[] value;

Command(int value) {
this.value = ByteBuffer.allocate(NUM_BYTES).putInt(value).array();
Command(long value) {
ByteValue.checkUnsignedBounds(value, NUM_BYTES);
this.value = ByteBuffer.allocate(NUM_BYTES)
.put((byte) (value >> 24 & 0xff))
.put((byte) (value >> 16 & 0xff))
.put((byte) (value >> 8 & 0xff))
.put((byte) (value & 0xff))
.array();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,26 @@ public class DataLength extends ByteValue {
assertLength(NUM_BYTES);
}

public static DataLength of(long length) {
checkUnsignedBounds(length, NUM_BYTES);
return new DataLength(ByteBuffer.allocate(NUM_BYTES)
.put((byte) (length >> 24 & 0xff))
.put((byte) (length >> 16 & 0xff))
.put((byte) (length >> 8 & 0xff))
.put((byte) (length & 0xff))
.array());
}

public static DataLength of(String length) {
return of(Long.parseLong(length));
}

public static DataLength of(byte... values) {
return new DataLength(values);
}

public static DataLength of(int length) {
return new DataLength(ByteBuffer.allocate(NUM_BYTES).putInt(length).array());
@Override
public String toString() {
return "" + (getBytes()[0] << 24 | getBytes()[1] << 16 | getBytes()[2] << 8 | getBytes()[3]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,26 @@ public class Length extends ByteValue {
assertLength(NUM_BYTES);
}

public static Length of(long length) {
checkUnsignedBounds(length, NUM_BYTES);
return new Length(ByteBuffer.allocate(NUM_BYTES)
.put((byte) (length >> 24 & 0xff))
.put((byte) (length >> 16 & 0xff))
.put((byte) (length >> 8 & 0xff))
.put((byte) (length & 0xff))
.array());
}

public static Length of(String length) {
return of(Long.parseLong(length));
}

public static Length of(byte... values) {
return new Length(values);
}
public static Length of(int length) {
return new Length(ByteBuffer.allocate(NUM_BYTES).putInt(length).array());

@Override
public String toString() {
return "" + (getBytes()[0] << 24 | getBytes()[1] << 16 | getBytes()[2] << 8 | getBytes()[3]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.apache.plc4x.java.ads.api.util.ByteReadable;
import org.apache.plc4x.java.ads.api.util.ByteValue;

import java.nio.ByteBuffer;

Expand Down Expand Up @@ -51,8 +52,14 @@ public enum State implements ByteReadable {

final byte[] value;

State(int value) {
this.value = ByteBuffer.allocate(NUM_BYTES).putInt(value).array();
State(long value) {
ByteValue.checkUnsignedBounds(value, NUM_BYTES);
this.value = ByteBuffer.allocate(NUM_BYTES)
.put((byte) (value >> 24 & 0xff))
.put((byte) (value >> 16 & 0xff))
.put((byte) (value >> 8 & 0xff))
.put((byte) (value & 0xff))
.array();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public ByteValue(byte... value) {
this.value = value;
}

protected void assertLength(int length) {
public void assertLength(int length) {
if (value.length != length) {
throw new IllegalArgumentException("Expected length " + length + " got " + value.length);
}
}

protected static void checkUnsignedBounds(long value, int numberOfBytes) {
public static void checkUnsignedBounds(long value, int numberOfBytes) {
double upperBound = Math.pow(2, 8 * numberOfBytes);
if (value < 0 || value >= upperBound) {
throw new IllegalArgumentException("Value must between 0 and " + upperBound + ". Was " + value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package org.apache.plc4x.java.ads.api.commands.types;

import org.apache.commons.codec.binary.Hex;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class SampleSizeTest {

byte NULL_BYTE = 0x0;

@Test
void ofBytes() {
Assertions.assertEquals("0", SampleSize.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString());
Assertions.assertThrows(IllegalArgumentException.class, () -> SampleSize.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE));
}

@Test
void ofLong() {
assertByte(SampleSize.of(1), "0x00000001");
assertByte(SampleSize.of(65535), "0x0000ffff");
Assertions.assertThrows(IllegalArgumentException.class, () -> SampleSize.of(-1));
Assertions.assertThrows(IllegalArgumentException.class, () -> SampleSize.of(Long.valueOf("4294967296")));
}

@Test
void ofString() {
assertByte(SampleSize.of("1"), "0x00000001");
}

@Test
void testToString() {
Assertions.assertEquals(SampleSize.of("1").toString(), "1");
}

void assertByte(SampleSize actual, String expected) {
Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes()));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package org.apache.plc4x.java.ads.api.commands.types;

import org.apache.commons.codec.binary.Hex;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class SamplesTest {

byte NULL_BYTE = 0x0;

@Test
void ofBytes() {
Assertions.assertEquals("0", Samples.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString());
Assertions.assertThrows(IllegalArgumentException.class, () -> Samples.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE));
}

@Test
void ofLong() {
assertByte(Samples.of(1), "0x00000001");
assertByte(Samples.of(65535), "0x0000ffff");
Assertions.assertThrows(IllegalArgumentException.class, () -> Samples.of(-1));
Assertions.assertThrows(IllegalArgumentException.class, () -> Samples.of(Long.valueOf("4294967296")));
}

@Test
void ofString() {
assertByte(Samples.of("1"), "0x00000001");
}

@Test
void testToString() {
Assertions.assertEquals(Samples.of("1").toString(), "1");
}

void assertByte(Samples actual, String expected) {
Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package org.apache.plc4x.java.ads.api.commands.types;

import org.apache.commons.codec.binary.Hex;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class WriteLengthTest {

byte NULL_BYTE = 0x0;

@Test
void ofBytes() {
Assertions.assertEquals("0", WriteLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString());
Assertions.assertThrows(IllegalArgumentException.class, () -> WriteLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE));
}

@Test
void ofLong() {
assertByte(WriteLength.of(1), "0x00000001");
assertByte(WriteLength.of(65535), "0x0000ffff");
Assertions.assertThrows(IllegalArgumentException.class, () -> WriteLength.of(-1));
Assertions.assertThrows(IllegalArgumentException.class, () -> WriteLength.of(Long.valueOf("4294967296")));
}

@Test
void ofString() {
assertByte(WriteLength.of("1"), "0x00000001");
}

@Test
void testToString() {
Assertions.assertEquals(WriteLength.of("1").toString(), "1");
}

void assertByte(WriteLength actual, String expected) {
Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes()));
}


}
Loading

0 comments on commit 8e0e8e6

Please sign in to comment.