Skip to content

Commit 78e7f43

Browse files
committed
Non working version of new type hierarchy
1 parent e5824b5 commit 78e7f43

File tree

4 files changed

+92
-33
lines changed

4 files changed

+92
-33
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2017 Patrick Favre-Bulle
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package at.favre.lib.bytes;
23+
24+
import java.nio.ByteOrder;
25+
26+
public abstract class AbstractBytes {
27+
28+
private final byte[] byteArray;
29+
private final ByteOrder byteOrder;
30+
31+
/**
32+
* Creates a new immutable instance
33+
*
34+
* @param byteArray internal byte array
35+
* @param byteOrder the internal byte order - this is used to interpret given array, not to change it
36+
*/
37+
public AbstractBytes(byte[] byteArray, ByteOrder byteOrder) {
38+
this.byteArray = byteArray;
39+
this.byteOrder = byteOrder;
40+
}
41+
42+
abstract BytesFactory getFactory();
43+
44+
public boolean isMutable() {
45+
return false;
46+
}
47+
48+
public boolean isReadOnly() {
49+
return false;
50+
}
51+
}

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
* </pre>
5757
*/
5858
@SuppressWarnings("WeakerAccess")
59-
public class Bytes implements Comparable<Bytes> {
59+
public class Bytes extends AbstractBytes implements Comparable<Bytes> {
6060

6161
/* FACTORY ***************************************************************************************************/
6262

@@ -381,42 +381,16 @@ public static Bytes random(int length, Random random) {
381381

382382
private final byte[] byteArray;
383383
private final ByteOrder byteOrder;
384-
private final boolean mutable;
385-
private final boolean readonly;
386384

387385
/**
388386
* Creates a new immutable instance
389387
*
390388
* @param byteArray internal byte array
391389
* @param byteOrder the internal byte order - this is used to interpret given array, not to change it
392390
*/
393-
Bytes(byte[] byteArray, ByteOrder byteOrder) {
394-
this(byteArray, byteOrder, false, false);
395-
}
396-
397-
/**
398-
* Creates a new instance with given array and copies all attributes from old instance
399-
*
400-
* @param byteArray internal byte array
401-
* @param oldInstance old instance to copy all internal attributes
402-
*/
403-
Bytes(byte[] byteArray, Bytes oldInstance) {
404-
this(byteArray, oldInstance.byteOrder(), oldInstance.mutable, oldInstance.readonly);
405-
}
406-
407-
/**
408-
* Creates a new instance
409-
*
410-
* @param byteArray internal byte array
411-
* @param byteOrder the internal byte order - this is used to interpret given array, not to change it
412-
* @param mutable if the internal state can be changed
413-
* @param readonly if all getter for internal byte array will fail
414-
*/
415-
Bytes(byte[] byteArray, ByteOrder byteOrder, boolean mutable, boolean readonly) {
391+
public Bytes(byte[] byteArray, ByteOrder byteOrder) {
416392
this.byteArray = byteArray;
417393
this.byteOrder = byteOrder;
418-
this.mutable = mutable;
419-
this.readonly = readonly;
420394
}
421395

422396
/* TRANSFORMER **********************************************************************************************/
@@ -692,7 +666,7 @@ public Bytes shuffle() {
692666
* @return the transformed instance (might be the same, or a new one)
693667
*/
694668
public Bytes transform(BytesTransformer transformer) {
695-
return transformer.transform(this, mutable);
669+
return transformer.transform(this, isMutable());
696670
}
697671

698672
/**
@@ -776,8 +750,9 @@ public ByteOrder byteOrder() {
776750
*
777751
* @return true if read only
778752
*/
753+
@Override
779754
public boolean isReadOnly() {
780-
return readonly;
755+
return false;
781756
}
782757

783758
/**
@@ -904,7 +879,7 @@ public Bytes byteOrder(ByteOrder byteOrder) {
904879
* @return a new instance if not already readonly, or "this" otherwise
905880
*/
906881
public Bytes readOnly() {
907-
if (readonly) {
882+
if (isReadOnly()) {
908883
return this;
909884
} else {
910885
return new Bytes(internalArray(), byteOrder, false, true);
@@ -980,7 +955,7 @@ public InputStream inputStream() {
980955
* @throws ReadOnlyBufferException if this is a read-only instance
981956
*/
982957
public byte[] array() {
983-
if (!readonly) {
958+
if (!isReadOnly()) {
984959
return internalArray();
985960
}
986961
throw new ReadOnlyBufferException();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2017 Patrick Favre-Bulle
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package at.favre.lib.bytes;
23+
24+
import java.nio.ByteOrder;
25+
26+
public interface BytesFactory {
27+
Bytes wrap(byte[] array, ByteOrder byteOrder);
28+
}

src/main/java/at/favre/lib/bytes/MutableBytes.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@
3434
public final class MutableBytes extends Bytes {
3535

3636
MutableBytes(byte[] byteArray, ByteOrder byteOrder) {
37-
super(byteArray, byteOrder, true, false);
37+
super(byteArray, byteOrder);
38+
}
39+
40+
@Override
41+
public boolean isMutable() {
42+
return true;
3843
}
3944

4045
/**

0 commit comments

Comments
 (0)