/*
 * Copyright 2017 enrico.olivelli.
 *
 * Licensed 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 herddb.utils;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * A container for strings. Data is decoded to a real java.lang.String only if needed
 *
 * @author enrico.olivelli
 */

public class RawString implements Comparable<RawString> {

    @SuppressFBWarnings(value = "EI_EXPOSE_REP2")
    public final byte[] data;
    private String string;
    private final int hashcode;

    public static RawString of(String string) {
        return new RawString(string.getBytes(StandardCharsets.UTF_8), string);
    }

    RawString(byte[] data, String s) {
        this(data);
        this.string = s;
    }

    public RawString(byte[] data) {
        this.data = data;
        this.hashcode = Arrays.hashCode(data);
    }

    @Override
    public int hashCode() {
        return hashcode;
    }

    @SuppressFBWarnings(value = "EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS")
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (obj instanceof RawString) {
            final RawString other = (RawString) obj;
            return (this.hashcode == other.hashcode)
                && Arrays.equals(this.data, other.data);
        }
        if (obj instanceof Boolean) {
            boolean b = (Boolean) obj;
            return b ? Arrays.equals(this.data, TRUE) : Arrays.equals(this.data, FALSE);
        }
        String otherString = obj.toString();
        byte[] other_data = otherString
            .getBytes(StandardCharsets.UTF_8);
        return Arrays.equals(this.data, other_data);
    }

    private static final byte[] TRUE = "true".getBytes(StandardCharsets.UTF_8);
    private static final byte[] FALSE = "false".getBytes(StandardCharsets.UTF_8);

    @Override
    public String toString() {
        String _string = string;
        if (_string != null) {
            return _string;
        }
        return string = new String(data, 0, data.length, StandardCharsets.UTF_8);
    }

    @Override
    public int compareTo(RawString o) {
        return compare(this.data, o.data);
    }

    public int compareToString(String o) {
        return compare(this.data, o.getBytes(StandardCharsets.UTF_8));
    }

    private static int compare(byte[] left, byte[] right) {
        for (int i = 0, j = 0; i < left.length && j < right.length; i++, j++) {
            int a = (left[i] & 0xff);
            int b = (right[j] & 0xff);
            if (a != b) {
                return a - b;
            }
        }
        return left.length - right.length;
    }

}

@@ -1,56 +1,234 @@
 /*%0A 
-* Copyright 2017 enrico.olivelli.%0A *%0A * Licensed
+Licensed to Diennea S.r.l. under one%0A or more contributor license agreements. See the NOTICE file%0A distributed with this work for additional information%0A regarding copyright ownership. Diennea S.r.l. licenses this file%0A to you
  und
@@ -266,16 +266,17 @@
 2.0 (the
+%0A
  %22Licens
@@ -279,19 +279,16 @@
 cense%22);
-%0A *
  you may
@@ -326,16 +326,17 @@
 mpliance
+%0A
  with th
@@ -345,19 +345,17 @@
 License.
-%0A *
+ 
  You may
@@ -391,18 +391,9 @@
  at%0A
- *%0A *     
+%0A
  htt
@@ -432,21 +432,17 @@
 NSE-2.0%0A
- *%0A *
+%0A
  Unless 
@@ -492,16 +492,17 @@
 writing,
+%0A
  softwar
@@ -502,19 +502,16 @@
 software
-%0A *
  distrib
@@ -553,16 +553,17 @@
 ed on an
+%0A
  %22AS IS%22
@@ -569,19 +569,16 @@
 %22 BASIS,
-%0A *
  WITHOUT
@@ -609,16 +609,17 @@
 S OF ANY
+%0A
  KIND, e
@@ -643,19 +643,17 @@
 implied.
-%0A *
+ 
  See the
@@ -668,16 +668,17 @@
  for the
+%0A
  specifi
@@ -713,19 +713,16 @@
 ions and
-%0A *
  limitat
@@ -725,16 +725,17 @@
 itations
+%0A
  under t
@@ -746,16 +746,17 @@
 icense.%0A
+%0A
  */%0Apack

fix header