/*
 * 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 com.facebook.presto.spi.type;

import com.fasterxml.jackson.annotation.JsonValue;

import java.util.Arrays;

import static java.util.Objects.requireNonNull;

public final class SqlVarbinary
        implements Comparable<SqlVarbinary>
{
    private final byte[] bytes;

    public SqlVarbinary(byte[] bytes)
    {
        this.bytes = requireNonNull(bytes, "bytes is null");
    }

    @Override
    public int compareTo(SqlVarbinary obj)
    {
        for (int i = 0; i < Math.min(bytes.length, obj.bytes.length); i++) {
            if (bytes[i] < obj.bytes[i]) {
                return -1;
            }
            else if (bytes[i] > obj.bytes[i]) {
                return 1;
            }
        }
        return bytes.length - obj.bytes.length;
    }

    @JsonValue
    public byte[] getBytes()
    {
        return bytes;
    }

    @Override
    public int hashCode()
    {
        return Arrays.hashCode(bytes);
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        SqlVarbinary other = (SqlVarbinary) obj;
        return Arrays.equals(bytes, other.bytes);
    }
}

@@ -799,16 +799,127 @@
 nary%3E%0A%7B%0A
+    private static final String BYTE_SEPARATOR = %22 %22;%0A    private static final String WORD_SEPARATOR = %22   %22;%0A%0A
     priv
@@ -1922,11 +1922,621 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public String toString()%0A    %7B%0A        StringBuilder builder = new StringBuilder();%0A%0A        for (int i = 0; i %3C bytes.length; ++i) %7B%0A            if (i != 0) %7B%0A                if (i %25 32 == 0) %7B%0A                    builder.append(%22%5Cn%22);%0A                %7D%0A                else if (i %25 8 == 0) %7B%0A                    builder.append(WORD_SEPARATOR);%0A                %7D%0A                else %7B%0A                    builder.append(BYTE_SEPARATOR);%0A                %7D%0A            %7D%0A%0A            builder.append(String.format(%22%2502x%22, bytes%5Bi%5D & 0xff));%0A        %7D%0A        return builder.toString();%0A    %7D
 %0A%7D%0A

Implement SqlVarbinary.toString() function.