keith-turner commented on a change in pull request #5: [WIP] DO NOT MERGE - 
Initial implementation
URL: https://github.com/apache/fluo-bytes/pull/5#discussion_r237194175
 
 

 ##########
 File path: src/main/java/org/apache/bytes/Bytes.java
 ##########
 @@ -0,0 +1,452 @@
+/*
+ * 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.bytes;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.lang.ref.WeakReference;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Objects;
+
+/**
+ * Represents bytes in Fluo. Bytes is an immutable wrapper around a byte 
array. Bytes always copies
+ * on creation and never lets its internal byte array escape. Its modeled 
after Java's String which
+ * is an immutable wrapper around a char array. It was created because there 
is nothing in Java like
+ * it at the moment. Its very nice having this immutable type, it avoids 
having to do defensive
+ * copies to ensure correctness. Maybe one day Java will have equivalents of 
String, StringBuilder,
+ * and Charsequence for bytes.
+ *
+ * <p>
+ * The reason Fluo did not use ByteBuffer is because its not immutable, even a 
read only ByteBuffer
+ * has a mutable position. This makes ByteBuffer unsuitable for place where an 
immutable data type
+ * is desirable, like a key for a map.
+ *
+ * <p>
+ * Bytes.EMPTY is used to represent a Bytes object with no data.
+ *
+ * @since 1.0.0
+ */
+public final class Bytes extends AbstractByteSequence implements 
Comparable<Bytes>, Serializable {
+
+  private static final long serialVersionUID = 1L;
+
+  private final byte[] data;
+
+  private transient WeakReference<String> utf8String;
+
+  public static final Bytes EMPTY = new Bytes(new byte[0]);
+
+  private int hashCode = 0;
+
+  public Bytes() {
 
 Review comment:
    If all constructors are package private then, the class is effectively 
final.  So we can drop final from the class definition.  This allows us the 
flexibility to subclass Bytes for internal implementations that have very 
different behaviors.  These internal package private subclasses could only be 
created by static methods.  This is the approach taken by protobuf.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to