// 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 array

import (
	
	
	
	

	
	
	
	
)

// Null represents an immutable, degenerate array with no physical storage.
type Null struct {
	array
}

// NewNull returns a new Null array value of size n.
func ( int) *Null {
	 := &Null{}
	.refCount.Add(1)
	 := NewData(
		arrow.Null, ,
		[]*memory.Buffer{nil},
		nil,
		,
		0,
	)
	.setData()
	.Release()
	return 
}

// NewNullData returns a new Null array value, from data.
func ( arrow.ArrayData) *Null {
	 := &Null{}
	.refCount.Add(1)
	.setData(.(*Data))
	return 
}

func ( *Null) (int) string { return NullValueStr }

func ( *Null) (int) interface{} { return nil }

func ( *Null) () string {
	 := new(strings.Builder)
	.WriteString("[")
	for  := 0;  < .Len(); ++ {
		if  > 0 {
			.WriteString(" ")
		}
		.WriteString(NullValueStr)
	}
	.WriteString("]")
	return .String()
}

func ( *Null) ( *Data) {
	.array.setData()
	.nullBitmapBytes = nil
	.data.nulls = .data.length
}

func ( *Null) ( int) interface{} {
	return nil
}

func ( *Null) () ([]byte, error) {
	return json.Marshal(make([]interface{}, .Len()))
}

type NullBuilder struct {
	builder
}

// NewNullBuilder returns a builder, using the provided memory allocator.
func ( memory.Allocator) *NullBuilder {
	 := &NullBuilder{builder: builder{mem: }}
	.refCount.Add(1)
	return 
}

func ( *NullBuilder) () arrow.DataType { return arrow.Null }

// Release decreases the reference count by 1.
// When the reference count goes to zero, the memory is freed.
func ( *NullBuilder) () {
	debug.Assert(.refCount.Load() > 0, "too many releases")

	if .refCount.Add(-1) == 0 {
		if .nullBitmap != nil {
			.nullBitmap.Release()
			.nullBitmap = nil
		}
	}
}

func ( *NullBuilder) () {
	.length++
	.nulls++
}

func ( *NullBuilder) ( int) {
	for  := 0;  < ; ++ {
		.AppendNull()
	}
}

func ( *NullBuilder) ( string) error {
	if  == NullValueStr {
		.AppendNull()
		return nil
	}
	return fmt.Errorf("cannot convert %q to null", )
}

func ( *NullBuilder) () { .AppendNull() }

func ( *NullBuilder) ( int) { .AppendNulls() }

func (*NullBuilder) ( int) {}
func (*NullBuilder) ( int)  {}

func (*NullBuilder) ( int)                       {}
func (*NullBuilder) ( int,  func(int)) {}

// NewArray creates a Null array from the memory buffers used by the builder and resets the NullBuilder
// so it can be used to build a new array.
func ( *NullBuilder) () arrow.Array {
	return .NewNullArray()
}

// NewNullArray creates a Null array from the memory buffers used by the builder and resets the NullBuilder
// so it can be used to build a new array.
func ( *NullBuilder) () ( *Null) {
	 := .newData()
	 = NewNullData()
	.Release()
	return
}

func ( *NullBuilder) () ( *Data) {
	 = NewData(
		arrow.Null, .length,
		[]*memory.Buffer{nil},
		nil,
		.nulls,
		0,
	)
	.reset()

	return
}

func ( *NullBuilder) ( *json.Decoder) error {
	,  := .Token()
	if  != nil {
		return 
	}

	switch .(type) {
	case nil:
		.AppendNull()
	default:
		return &json.UnmarshalTypeError{
			Value:  fmt.Sprint(),
			Type:   reflect.TypeOf(nil),
			Offset: .InputOffset(),
		}
	}
	return nil
}

func ( *NullBuilder) ( *json.Decoder) error {
	for .More() {
		if  := .UnmarshalOne();  != nil {
			return 
		}
	}
	return nil
}

func ( *NullBuilder) ( []byte) error {
	 := json.NewDecoder(bytes.NewReader())
	,  := .Token()
	if  != nil {
		return 
	}

	if ,  := .(json.Delim); ! ||  != '[' {
		return fmt.Errorf("null builder must unpack from json array, found %s", )
	}

	return .Unmarshal()
}

var (
	_ arrow.Array = (*Null)(nil)
	_ Builder     = (*NullBuilder)(nil)
)