|
| 1 | +// Copyright 2019 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:meta/meta.dart'; |
| 6 | + |
| 7 | +/// A definition of a time range, which can be specific or indefinite. |
| 8 | +@immutable |
| 9 | +sealed class TimeRange { |
| 10 | + const TimeRange(); |
| 11 | + |
| 12 | + /// Matches any time. |
| 13 | + static const TimeRange indefinite = IndefiniteTimeRange(); |
| 14 | + |
| 15 | + /// Creates a range matching time between [start] and [end]. |
| 16 | + /// |
| 17 | + /// If [exclusive] is set, the specific moments described in [start] and [end] |
| 18 | + /// are excluded. |
| 19 | + /// |
| 20 | + /// [end] must be after [start]. |
| 21 | + factory TimeRange.between( |
| 22 | + DateTime start, // |
| 23 | + DateTime end, { |
| 24 | + bool exclusive, |
| 25 | + }) = SpecificTimeRange.between; |
| 26 | + |
| 27 | + /// Creates a range matching time before [end]. |
| 28 | + /// |
| 29 | + /// If [exclusive] is set, the specific moment described in [end] is excluded. |
| 30 | + factory TimeRange.before( |
| 31 | + DateTime end, { // |
| 32 | + bool exclusive, |
| 33 | + }) = SpecificTimeRange.before; |
| 34 | + |
| 35 | + /// Creates a range matching time after [start]. |
| 36 | + /// |
| 37 | + /// If [exclusive] is set, the specific moment described in [start] is |
| 38 | + /// excluded. |
| 39 | + factory TimeRange.after( |
| 40 | + DateTime start, { // |
| 41 | + bool exclusive, |
| 42 | + }) = SpecificTimeRange.after; |
| 43 | + |
| 44 | + @override |
| 45 | + @nonVirtual |
| 46 | + bool operator ==(Object other) { |
| 47 | + return other is TimeRange && start == other.start && end == other.end; |
| 48 | + } |
| 49 | + |
| 50 | + @override |
| 51 | + @nonVirtual |
| 52 | + int get hashCode => Object.hash(start, end); |
| 53 | + |
| 54 | + /// A starting point in time. |
| 55 | + /// |
| 56 | + /// If `null`, the time range is infinite in the past. |
| 57 | + DateTime? get start; |
| 58 | + |
| 59 | + /// An ending point in time. |
| 60 | + /// |
| 61 | + /// If `null`, the time range is infinite in the future. |
| 62 | + DateTime? get end; |
| 63 | + |
| 64 | + /// Returns whether [time] is within `this` range. |
| 65 | + bool contains(DateTime time); |
| 66 | + |
| 67 | + @override |
| 68 | + String toString() { |
| 69 | + if (start == null && end == null) { |
| 70 | + return 'TimeRange.indefinite'; |
| 71 | + } |
| 72 | + if (start == null) { |
| 73 | + return 'TimeRange.before($end)'; |
| 74 | + } |
| 75 | + if (end == null) { |
| 76 | + return 'TimeRange.after($start)'; |
| 77 | + } |
| 78 | + return 'TimeRange.between($start, $end)'; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// No specific start or end time. |
| 83 | +final class IndefiniteTimeRange extends TimeRange { |
| 84 | + @literal |
| 85 | + const IndefiniteTimeRange(); |
| 86 | + |
| 87 | + @override |
| 88 | + Null get start => null; |
| 89 | + |
| 90 | + @override |
| 91 | + Null get end => null; |
| 92 | + |
| 93 | + @override |
| 94 | + bool contains(DateTime time) => true; |
| 95 | +} |
| 96 | + |
| 97 | +/// A specific time range, which includes at _least_ [start] or [end], or both. |
| 98 | +final class SpecificTimeRange extends TimeRange { |
| 99 | + /// Creates a time range with a specific [end] time. |
| 100 | + /// |
| 101 | + /// If [exclusive] is set, the specific moment described in [end] is excluded. |
| 102 | + SpecificTimeRange.before( |
| 103 | + DateTime this.end, { // |
| 104 | + this.exclusive = false, |
| 105 | + }) : start = null; |
| 106 | + |
| 107 | + /// Creates a time range with a specific [start] time. |
| 108 | + /// |
| 109 | + /// If [exclusive] is set, the specific moment described in [start] is |
| 110 | + /// excluded. |
| 111 | + SpecificTimeRange.after( |
| 112 | + DateTime this.start, { // |
| 113 | + this.exclusive = false, |
| 114 | + }) : end = null; |
| 115 | + |
| 116 | + /// Creates a time range with a specific [start] and [end] time. |
| 117 | + /// |
| 118 | + /// If [exclusive] is set, the specific moment described in [start] and [end] |
| 119 | + /// are excluded. |
| 120 | + /// |
| 121 | + /// [start] must be before [end]. |
| 122 | + SpecificTimeRange.between( |
| 123 | + DateTime this.start, // |
| 124 | + DateTime this.end, { |
| 125 | + this.exclusive = false, |
| 126 | + }) { |
| 127 | + if (start!.isAfter(end!)) { |
| 128 | + throw ArgumentError.value( |
| 129 | + start, |
| 130 | + 'start', |
| 131 | + 'Start time must be before end time.', |
| 132 | + ); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /// Whether [start], or [end], if specified, should exclude that instant. |
| 137 | + final bool exclusive; |
| 138 | + |
| 139 | + /// Whether [start], or [end], if specified, should include that instant. |
| 140 | + bool get inclusive => !exclusive; |
| 141 | + |
| 142 | + @override |
| 143 | + final DateTime? start; |
| 144 | + |
| 145 | + @override |
| 146 | + final DateTime? end; |
| 147 | + |
| 148 | + @override |
| 149 | + bool contains(DateTime time) { |
| 150 | + if (start case final start? when start.isAfter(time)) { |
| 151 | + return false; |
| 152 | + } |
| 153 | + if (end case final end? when end.isBefore(time)) { |
| 154 | + return false; |
| 155 | + } |
| 156 | + if (exclusive) { |
| 157 | + return time != start && time != end; |
| 158 | + } |
| 159 | + return true; |
| 160 | + } |
| 161 | +} |
0 commit comments