Change the static castTo methods to be named castFrom.

It's just a better name.

Bug:
Change-Id: I0bf42b53fb5041faa307d288ffe12fb4b1ac7068
Reviewed-on: https://dart-review.googlesource.com/34180
Reviewed-by: Leaf Petersen <leafp@google.com>
This commit is contained in:
Lasse R.H. Nielsen 2018-01-15 08:59:31 +00:00
parent e01081ef03
commit 6178cc17ed
6 changed files with 31 additions and 14 deletions

View file

@ -31,6 +31,23 @@ abstract class Queue<E> implements EfficientLengthIterable<E> {
*/
factory Queue.from(Iterable elements) = ListQueue<E>.from;
/**
* Adapts [source] to be a `Queue<T>`.
*
* Any time the queue would produce an element that is not a [T],
* the element access will throw.
*
* Any time a [T] value is attempted stored into the adapted queue,
* the store will throw unless the value is also an instance of [S].
*
* If all accessed elements of [source] are actually instances of [T],
* and if all elements stored into the returned queue are actually instance
* of [S],
* then the returned queue can be used as a `Queue<T>`.
*/
static Queue<T> castFrom<S, T>(Queue<S> source) =>
new CastQueue<S, T>(source);
/**
* Removes and returns the first element of this queue.
*

View file

@ -118,7 +118,7 @@ abstract class Iterable<E> {
* instances of [T], or if only elements that are actually instances of [T]
* are accessed, then the resulting iterable can be used as an `Iterable<T>`.
*/
static Iterable<T> castTo<S, T>(Iterable<S> source) =>
static Iterable<T> castFrom<S, T>(Iterable<S> source) =>
new CastIterable<S, T>(source);
/**

View file

@ -169,7 +169,7 @@ abstract class List<E> implements EfficientLengthIterable<E> {
* of [S],
* then the returned list can be used as a `List<T>`.
*/
static List<T> castTo<S, T>(List<S> source) => new CastList<S, T>(source);
static List<T> castFrom<S, T>(List<S> source) => new CastList<S, T>(source);
/**
* Returns the object at the given [index] in the list

View file

@ -156,7 +156,7 @@ abstract class Map<K, V> {
* and if all entries added to the returned map have [K] keys and [V]] values,
* then the returned map can be used as a `Map<K2, V2>`.
*/
static Map<K2, V2> castTo<K, V, K2, V2>(Map<K, V> source) =>
static Map<K2, V2> castFrom<K, V, K2, V2>(Map<K, V> source) =>
new CastMap<K, V, K2, V2>(source);
/**

View file

@ -98,7 +98,7 @@ abstract class Set<E> extends EfficientLengthIterable<E> {
* of [S],
* then the returned set can be used as a `Set<T>`.
*/
static Set<T> castTo<S, T>(Set<S> source, {Set<R> Function<R>() newSet}) =>
static Set<T> castFrom<S, T>(Set<S> source, {Set<R> Function<R>() newSet}) =>
new CastSet<S, T>(source, newSet);
/**

View file

@ -18,7 +18,7 @@ void testIterable() {
// Down-cast
{
// An iterable that (likely) can do direct access.
var dIterable = Iterable.castTo<C, D>(iterable);
var dIterable = Iterable.castFrom<C, D>(iterable);
Expect.throws(() => dIterable.first, null, "1.first");
Expect.equals(d, dIterable.elementAt(1));
@ -31,7 +31,7 @@ void testIterable() {
{
// An iterable that cannot do direct access.
var dIterable2 = Iterable.castTo<C, D>(iterable.where((_) => true));
var dIterable2 = Iterable.castFrom<C, D>(iterable.where((_) => true));
Expect.throws(() => dIterable2.first, null, "2.first");
Expect.equals(d, dIterable2.elementAt(1));
@ -46,7 +46,7 @@ void testIterable() {
// Iterable that definitely won't survive accessing element 2.
var iterable3 = new Iterable<C>.generate(
elements.length, (n) => n == 3 ? throw "untouchable" : elements[n]);
var dIterable3 = Iterable.castTo<C, D>(iterable3);
var dIterable3 = Iterable.castFrom<C, D>(iterable3);
Expect.throws(() => dIterable3.first, null, "3.first");
Expect.equals(d, dIterable3.elementAt(1));
@ -60,7 +60,7 @@ void testIterable() {
// Up-cast.
{
var oIterable4 = Iterable.castTo<C, Object>(iterable);
var oIterable4 = Iterable.castFrom<C, Object>(iterable);
Expect.listEquals(elements, oIterable4.toList());
}
}
@ -68,7 +68,7 @@ void testIterable() {
void testList() {
// Down-cast.
var list = new List<C>.from(elements);
var dList = List.castTo<C, D>(list);
var dList = List.castFrom<C, D>(list);
Expect.throws(() => dList.first); // C is not D.
Expect.equals(d, dList[1]);
@ -83,7 +83,7 @@ void testList() {
// Up-cast.
var list2 = new List<C>.from(elements);
var dList2 = List.castTo<C, Object>(list2);
var dList2 = List.castFrom<C, Object>(list2);
Expect.listEquals(elements, dList2);
Expect.throws(() => dList2[2] = new Object()); // Cannot set non-C.
Expect.listEquals(elements, dList2);
@ -93,7 +93,7 @@ void testSet() {
var set = new Set<C>.from(elements); // Linked HashSet.
Expect.listEquals(elements, set.toList()); // Preserves order.
var dSet = Set.castTo<C, D>(set);
var dSet = Set.castFrom<C, D>(set);
// Preserves order.
Expect.throws(() => dSet.first); // C is not D.
@ -116,7 +116,7 @@ void testSet() {
// Up-cast
var set2 = new Set<C>.from(elements);
var dSet2 = Set.castTo<C, Object>(set2);
var dSet2 = Set.castFrom<C, Object>(set2);
var newObject = new Object();
Expect.throws(() => dSet2.add(newObject));
@ -129,7 +129,7 @@ void testSet() {
// Custom emptySet.
var set3 = new Set<C>.from(elements);
var dSet3 = Set.castTo<C, Object>(set3, newSet: <T>() => new HashSet<T>());
var dSet3 = Set.castFrom<C, Object>(set3, newSet: <T>() => new HashSet<T>());
var toSet3 = dSet3.toSet();
Expect.isTrue(toSet3 is HashSet<Object>);
@ -140,7 +140,7 @@ void testSet() {
void testMap() {
var map = new Map.fromIterables(elements, elements);
var dMap = Map.castTo<C, C, D, D>(map);
var dMap = Map.castFrom<C, C, D, D>(map);
Expect.isTrue(dMap is Map<D, D>);