// 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.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Apache.Arrow
{
///
/// The class is an abstract array builder that can
/// accept dates in the form of or and convert to some
/// underlying date representation.
///
public abstract class DateArrayBuilder :
DelegatingArrayBuilder,
IArrowArrayBuilder,
IArrowArrayBuilder
where TArray : IArrowArray
where TBuilder : class, IArrowArrayBuilder
{
///
/// Construct a new instance of the class.
///
/// Inner builder that will produce arrays of type .
///
protected DateArrayBuilder(IArrowArrayBuilder> innerBuilder)
: base(innerBuilder)
{ }
///
/// Append a date in the form of a object to the array.
///
///
/// The value of on the input does not have any effect on the behaviour of this
/// method.
///
/// Date to add.
/// Returns the builder (for fluent-style composition).
public TBuilder Append(DateTime value)
{
InnerBuilder.Append(Convert(value));
return this as TBuilder;
}
///
/// Append a date from a object to the array.
///
///
/// Note that to convert the supplied parameter to a date, it is first converted to
/// UTC and the date then taken from the UTC date/time. Depending on the value of its
/// property, this may not necessarily be the same as the date obtained by
/// calling its property.
///
/// Date to add.
/// Returns the builder (for fluent-style composition).
public TBuilder Append(DateTimeOffset value)
{
InnerBuilder.Append(Convert(value));
return this as TBuilder;
}
///
/// Append a span of dates in the form of objects to the array.
///
///
/// The value of on any of the inputs does not have any effect on the behaviour of
/// this method.
///
/// Span of dates to add.
/// Returns the builder (for fluent-style composition).
public TBuilder Append(ReadOnlySpan span)
{
InnerBuilder.Reserve(span.Length);
foreach (var item in span)
{
InnerBuilder.Append(Convert(item));
}
return this as TBuilder;
}
///
/// Append a span of dates in the form of objects to the array.
///
///
/// Note that to convert the objects in the parameter to
/// dates, they are first converted to UTC and the date then taken from the UTC date/times. Depending on the
/// value of each property, this may not necessarily be the same as the
/// date obtained by calling the property.
///
/// Span of dates to add.
/// Returns the builder (for fluent-style composition).
public TBuilder Append(ReadOnlySpan span)
{
InnerBuilder.Reserve(span.Length);
foreach (var item in span)
{
InnerBuilder.Append(Convert(item));
}
return this as TBuilder;
}
///
/// Append a null date to the array.
///
/// Returns the builder (for fluent-style composition).
public TBuilder AppendNull()
{
InnerBuilder.AppendNull();
return this as TBuilder;
}
///
/// Append a collection of dates in the form of objects to the array.
///
///
/// The value of on any of the inputs does not have any effect on the behaviour of
/// this method.
///
/// Collection of dates to add.
/// Returns the builder (for fluent-style composition).
public TBuilder AppendRange(IEnumerable values)
{
InnerBuilder.AppendRange(values.Select(Convert));
return this as TBuilder;
}
///
/// Append a collection of dates in the form of objects to the array.
///
///
/// Note that to convert the objects in the parameter to
/// dates, they are first converted to UTC and the date then taken from the UTC date/times. Depending on the
/// value of each property, this may not necessarily be the same as the
/// date obtained by calling the property.
///
/// Collection of dates to add.
/// Returns the builder (for fluent-style composition).
public TBuilder AppendRange(IEnumerable values)
{
InnerBuilder.AppendRange(values.Select(Convert));
return this as TBuilder;
}
///
/// Set the value of a date in the form of a object at the specified index.
///
///
/// The value of on the input does not have any effect on the behaviour of this
/// method.
///
/// Index at which to set value.
/// Date to set.
/// Returns the builder (for fluent-style composition).
public TBuilder Set(int index, DateTime value)
{
InnerBuilder.Set(index, Convert(value));
return this as TBuilder;
}
///
/// Set the value of a date in the form of a object at the specified index.
///
///
/// Note that to convert the supplied parameter to a date, it is first converted to
/// UTC and the date then taken from the UTC date/time. Depending on the value of its
/// property, this may not necessarily be the same as the date obtained by
/// calling its property.
///
/// Index at which to set value.
/// Date to set.
/// Returns the builder (for fluent-style composition).
public TBuilder Set(int index, DateTimeOffset value)
{
InnerBuilder.Set(index, Convert(value));
return this as TBuilder;
}
///
/// Swap the values of the dates at the specified indices.
///
/// First index.
/// Second index.
/// Returns the builder (for fluent-style composition).
public TBuilder Swap(int i, int j)
{
InnerBuilder.Swap(i, j);
return this as TBuilder;
}
protected abstract TUnderlying Convert(DateTime dateTime);
protected abstract TUnderlying Convert(DateTimeOffset dateTimeOffset);
}
}