diff --git a/src/Apache.Arrow/Types/DurationType.cs b/src/Apache.Arrow/Types/DurationType.cs index 7e937a6e..9a725fb7 100644 --- a/src/Apache.Arrow/Types/DurationType.cs +++ b/src/Apache.Arrow/Types/DurationType.cs @@ -13,6 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System; + namespace Apache.Arrow.Types { public sealed class DurationType : TimeBasedType @@ -21,7 +23,6 @@ public sealed class DurationType : TimeBasedType public static readonly DurationType Millisecond = new DurationType(TimeUnit.Millisecond); public static readonly DurationType Microsecond = new DurationType(TimeUnit.Microsecond); public static readonly DurationType Nanosecond = new DurationType(TimeUnit.Nanosecond); - private static readonly DurationType[] _types = new DurationType[] { Second, Millisecond, Microsecond, Nanosecond }; private DurationType(TimeUnit unit) : base(unit) @@ -32,10 +33,14 @@ private DurationType(TimeUnit unit) public override string Name => "duration"; public override int BitWidth => 64; - public static DurationType FromTimeUnit(TimeUnit unit) + public static DurationType FromTimeUnit(TimeUnit unit) => unit switch { - return _types[(int)unit]; - } + TimeUnit.Second => Second, + TimeUnit.Millisecond => Millisecond, + TimeUnit.Microsecond => Microsecond, + TimeUnit.Nanosecond => Nanosecond, + _ => throw new ArgumentOutOfRangeException(nameof(unit), unit, @"Unsupported time unit.") + }; public override void Accept(IArrowTypeVisitor visitor) => Accept(this, visitor); } diff --git a/src/Apache.Arrow/Types/IntervalType.cs b/src/Apache.Arrow/Types/IntervalType.cs index b8e1fdea..14bf61f1 100644 --- a/src/Apache.Arrow/Types/IntervalType.cs +++ b/src/Apache.Arrow/Types/IntervalType.cs @@ -22,7 +22,6 @@ public sealed class IntervalType : FixedWidthType public static readonly IntervalType YearMonth = new IntervalType(IntervalUnit.YearMonth); public static readonly IntervalType DayTime = new IntervalType(IntervalUnit.DayTime); public static readonly IntervalType MonthDayNanosecond = new IntervalType(IntervalUnit.MonthDayNanosecond); - private static readonly IntervalType[] _types = new IntervalType[] { YearMonth, DayTime, MonthDayNanosecond }; public override ArrowTypeId TypeId => ArrowTypeId.Interval; public override string Name => "interval"; @@ -43,9 +42,12 @@ public IntervalType(IntervalUnit unit = IntervalUnit.YearMonth) public override void Accept(IArrowTypeVisitor visitor) => Accept(this, visitor); - public static IntervalType FromIntervalUnit(IntervalUnit unit) + public static IntervalType FromIntervalUnit(IntervalUnit unit) => unit switch { - return _types[(int)unit]; - } + IntervalUnit.YearMonth => YearMonth, + IntervalUnit.DayTime => DayTime, + IntervalUnit.MonthDayNanosecond => MonthDayNanosecond, + _ => throw new ArgumentOutOfRangeException(nameof(unit), unit, @"Unsupported interval unit") + }; } } diff --git a/src/Apache.Arrow/Types/TimeType.cs b/src/Apache.Arrow/Types/TimeType.cs index b317df26..2ca612cc 100644 --- a/src/Apache.Arrow/Types/TimeType.cs +++ b/src/Apache.Arrow/Types/TimeType.cs @@ -13,6 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System; + namespace Apache.Arrow.Types { public abstract class TimeType : TimeBasedType @@ -21,16 +23,19 @@ public abstract class TimeType : TimeBasedType public static readonly Time32Type Millisecond = new Time32Type(TimeUnit.Millisecond); public static readonly Time64Type Microsecond = new Time64Type(TimeUnit.Microsecond); public static readonly Time64Type Nanosecond = new Time64Type(TimeUnit.Nanosecond); - private static readonly TimeType[] _types = new TimeType[] { Second, Millisecond, Microsecond, Nanosecond }; protected TimeType(TimeUnit unit) : base(unit) { } - public static TimeType FromTimeUnit(TimeUnit unit) + public static TimeType FromTimeUnit(TimeUnit unit) => unit switch { - return _types[(int)unit]; - } + TimeUnit.Second => Second, + TimeUnit.Millisecond => Millisecond, + TimeUnit.Microsecond => Microsecond, + TimeUnit.Nanosecond => Nanosecond, + _ => throw new ArgumentOutOfRangeException(nameof(unit), unit, @"Unsupported time unit") + }; } }