Skip to content

Commit 77d4167

Browse files
authored
Use quiver's concat for iterables in Class construction and eliminate duplicitous array/set creation and sorting (#1847)
1 parent 009daa9 commit 77d4167

File tree

3 files changed

+67
-128
lines changed

3 files changed

+67
-128
lines changed

lib/src/html/html_generator_instance.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,17 @@ class HtmlGeneratorInstance {
148148
generateProperty(_packageGraph, lib, clazz, property);
149149
}
150150

151-
for (var property in filterNonDocumented(clazz.propertiesForPages)) {
151+
for (var property in filterNonDocumented(clazz.allInstanceFields)) {
152152
if (!property.isCanonical) continue;
153153
generateProperty(_packageGraph, lib, clazz, property);
154154
}
155155

156-
for (var method in filterNonDocumented(clazz.methodsForPages)) {
156+
for (var method in filterNonDocumented(clazz.allInstanceMethods)) {
157157
if (!method.isCanonical) continue;
158158
generateMethod(_packageGraph, lib, clazz, method);
159159
}
160160

161-
for (var operator in filterNonDocumented(clazz.operatorsForPages)) {
161+
for (var operator in filterNonDocumented(clazz.allOperators)) {
162162
if (!operator.isCanonical) continue;
163163
generateMethod(_packageGraph, lib, clazz, operator);
164164
}
@@ -186,17 +186,17 @@ class HtmlGeneratorInstance {
186186
generateProperty(_packageGraph, lib, mixin, property);
187187
}
188188

189-
for (var property in filterNonDocumented(mixin.propertiesForPages)) {
189+
for (var property in filterNonDocumented(mixin.allInstanceFields)) {
190190
if (!property.isCanonical) continue;
191191
generateProperty(_packageGraph, lib, mixin, property);
192192
}
193193

194-
for (var method in filterNonDocumented(mixin.methodsForPages)) {
194+
for (var method in filterNonDocumented(mixin.allInstanceMethods)) {
195195
if (!method.isCanonical) continue;
196196
generateMethod(_packageGraph, lib, mixin, method);
197197
}
198198

199-
for (var operator in filterNonDocumented(mixin.operatorsForPages)) {
199+
for (var operator in filterNonDocumented(mixin.allOperators)) {
200200
if (!operator.isCanonical) continue;
201201
generateMethod(_packageGraph, lib, mixin, operator);
202202
}
@@ -209,13 +209,13 @@ class HtmlGeneratorInstance {
209209

210210
for (var eNum in filterNonDocumented(lib.enums)) {
211211
generateEnum(_packageGraph, lib, eNum);
212-
for (var property in filterNonDocumented(eNum.propertiesForPages)) {
212+
for (var property in filterNonDocumented(eNum.allInstanceFields)) {
213213
generateProperty(_packageGraph, lib, eNum, property);
214214
}
215-
for (var operator in filterNonDocumented(eNum.operatorsForPages)) {
215+
for (var operator in filterNonDocumented(eNum.allOperators)) {
216216
generateMethod(_packageGraph, lib, eNum, operator);
217217
}
218-
for (var method in filterNonDocumented(eNum.methodsForPages)) {
218+
for (var method in filterNonDocumented(eNum.allInstanceMethods)) {
219219
generateMethod(_packageGraph, lib, eNum, method);
220220
}
221221
}

lib/src/model.dart

Lines changed: 30 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class InheritableAccessor extends Accessor with Inheritable {
374374
Class parentClass =
375375
new ModelElement.fromElement(t.element, packageGraph);
376376
List<Field> possibleFields = [];
377-
possibleFields.addAll(parentClass.allInstanceProperties);
377+
possibleFields.addAll(parentClass.allInstanceFields);
378378
possibleFields.addAll(parentClass.staticProperties);
379379
String fieldName = accessor.name.replaceFirst('=', '');
380380
Field foundField = possibleFields.firstWhere(
@@ -417,7 +417,7 @@ class Accessor extends ModelElement implements EnclosedElement {
417417
// enclosingCombo always gets set at accessor creation time, somehow, to
418418
// avoid this.
419419
// TODO(jcollins-g): This also doesn't work for private accessors sometimes.
420-
(enclosingElement as Class).allFields;
420+
(enclosingElement as Class).allInstanceFields;
421421
}
422422
assert(_enclosingCombo != null);
423423
}
@@ -598,19 +598,14 @@ class Class extends ModelElement
598598
List<Method> _allMethods;
599599
List<Operator> _operators;
600600
List<Operator> _inheritedOperators;
601-
List<Operator> _allOperators;
602-
final Set<Operator> _genPageOperators = new Set();
603601
List<Method> _inheritedMethods;
604602
List<Method> _staticMethods;
605603
List<Method> _instanceMethods;
606-
List<Method> _allInstanceMethods;
607-
final Set<Method> _genPageMethods = new Set();
608604
List<Field> _fields;
609605
List<Field> _staticFields;
610606
List<Field> _constants;
611607
List<Field> _instanceFields;
612608
List<Field> _inheritedProperties;
613-
List<Field> _allInstanceProperties;
614609

615610
Class(ClassElement element, Library library, PackageGraph packageGraph)
616611
: super(element, library, packageGraph, null) {
@@ -632,66 +627,25 @@ class Class extends ModelElement
632627
.toList(growable: false);
633628
}
634629

635-
List<Method> get allInstanceMethods {
636-
if (_allInstanceMethods != null) return _allInstanceMethods;
637-
_allInstanceMethods = []
638-
..addAll([]
639-
..addAll(instanceMethods)
640-
..sort(byName))
641-
..addAll([]
642-
..addAll(inheritedMethods)
643-
..sort(byName));
644-
return _allInstanceMethods;
645-
}
630+
Iterable<Method> get allInstanceMethods => quiverIterables.concat([instanceMethods, inheritedMethods]);
646631

647632
Iterable<Method> get allPublicInstanceMethods =>
648633
filterNonPublic(allInstanceMethods);
649634

650635
bool get allPublicInstanceMethodsInherited =>
651636
instanceMethods.every((f) => f.isInherited);
652637

653-
List<Field> get allInstanceProperties {
654-
if (_allInstanceProperties != null) return _allInstanceProperties;
638+
Iterable<Field> get allInstanceFields => quiverIterables.concat([instanceProperties, inheritedProperties]);
655639

656-
// TODO best way to make this a fixed length list?
657-
_allInstanceProperties = []
658-
..addAll([]
659-
..addAll(instanceProperties)
660-
..sort(byName))
661-
..addAll([]
662-
..addAll(inheritedProperties)
663-
..sort(byName));
664-
return _allInstanceProperties;
665-
}
666-
667-
Iterable<Accessor> get allAccessors {
668-
return []
669-
..addAll(allInstanceProperties.expand((f) {
670-
List<Accessor> getterSetters = [];
671-
if (f.hasGetter) getterSetters.add(f.getter);
672-
if (f.hasSetter) getterSetters.add(f.setter);
673-
return getterSetters;
674-
}))
675-
..addAll(constants.map<Accessor>((c) => c.getter));
676-
}
640+
Iterable<Accessor> get allAccessors => quiverIterables.concat([allInstanceFields.expand((f) => f.allAccessors), constants.map((c) => c.getter)]);
677641

678642
Iterable<Field> get allPublicInstanceProperties =>
679-
filterNonPublic(allInstanceProperties);
643+
filterNonPublic(allInstanceFields);
680644

681645
bool get allPublicInstancePropertiesInherited =>
682646
allPublicInstanceProperties.every((f) => f.isInherited);
683647

684-
List<Operator> get allOperators {
685-
if (_allOperators != null) return _allOperators;
686-
_allOperators = []
687-
..addAll([]
688-
..addAll(operators)
689-
..sort(byName))
690-
..addAll([]
691-
..addAll(inheritedOperators)
692-
..sort(byName));
693-
return _allOperators;
694-
}
648+
Iterable<Operator> get allOperators => quiverIterables.concat([operators, inheritedOperators]);
695649

696650
Iterable<Operator> get allPublicOperators => filterNonPublic(allOperators);
697651

@@ -700,7 +654,7 @@ class Class extends ModelElement
700654

701655
List<Field> get constants {
702656
if (_constants != null) return _constants;
703-
_constants = allFields.where((f) => f.isConst).toList(growable: false)
657+
_constants = _allFields.where((f) => f.isConst).toList(growable: false)
704658
..sort(byName);
705659

706660
return _constants;
@@ -760,7 +714,7 @@ class Class extends ModelElement
760714
_allModelElements = new List.from(
761715
quiverIterables.concat([
762716
allInstanceMethods,
763-
allInstanceProperties,
717+
allInstanceFields,
764718
allAccessors,
765719
allOperators,
766720
constants,
@@ -861,7 +815,7 @@ class Class extends ModelElement
861815

862816
List<Method> get inheritedMethods {
863817
if (_inheritedMethods == null) {
864-
_inheritedMethods = new List<Method>();
818+
_inheritedMethods = <Method>[];
865819
Set<String> methodNames = _methods.map((m) => m.element.name).toSet();
866820

867821
Set<ExecutableElement> inheritedMethodElements =
@@ -876,7 +830,6 @@ class Class extends ModelElement
876830
Method m = new ModelElement.from(e, library, packageGraph,
877831
enclosingClass: this);
878832
_inheritedMethods.add(m);
879-
_genPageMethods.add(m);
880833
}
881834
_inheritedMethods.sort(byName);
882835
}
@@ -902,7 +855,6 @@ class Class extends ModelElement
902855
Operator o = new ModelElement.from(e, library, packageGraph,
903856
enclosingClass: this);
904857
_inheritedOperators.add(o);
905-
_genPageOperators.add(o);
906858
}
907859
_inheritedOperators.sort(byName);
908860
}
@@ -914,7 +866,7 @@ class Class extends ModelElement
914866

915867
List<Field> get inheritedProperties {
916868
if (_inheritedProperties == null) {
917-
_inheritedProperties = allFields.where((f) => f.isInherited).toList()
869+
_inheritedProperties = _allFields.where((f) => f.isInherited).toList()
918870
..sort(byName);
919871
}
920872
return _inheritedProperties;
@@ -930,16 +882,14 @@ class Class extends ModelElement
930882
.where((m) => !m.isStatic && !m.isOperator)
931883
.toList(growable: false)
932884
..sort(byName);
933-
934-
_genPageMethods.addAll(_instanceMethods);
935885
return _instanceMethods;
936886
}
937887

938888
Iterable<Method> get publicInstanceMethods => instanceMethods;
939889

940890
List<Field> get instanceProperties {
941891
if (_instanceFields != null) return _instanceFields;
942-
_instanceFields = allFields
892+
_instanceFields = _allFields
943893
.where((f) => !f.isStatic && !f.isInherited && !f.isConst)
944894
.toList(growable: false)
945895
..sort(byName);
@@ -989,8 +939,6 @@ class Class extends ModelElement
989939
@override
990940
String get kind => 'class';
991941

992-
List<Method> get methodsForPages => _genPageMethods.toList(growable: false);
993-
994942
List<DefinedElementType> get mixins => _mixins;
995943

996944
Iterable<DefinedElementType> get publicMixins => filterNonPublic(mixins);
@@ -1005,30 +953,11 @@ class Class extends ModelElement
1005953
.cast<Operator>()
1006954
.toList(growable: false)
1007955
..sort(byName);
1008-
_genPageOperators.addAll(_operators);
1009-
1010956
return _operators;
1011957
}
1012958

1013959
Iterable<Operator> get publicOperators => filterNonPublic(operators);
1014960

1015-
List<Operator> get operatorsForPages =>
1016-
new UnmodifiableListView(_genPageOperators.toList());
1017-
1018-
// TODO: make this method smarter about hierarchies and overrides. Right
1019-
// now, we're creating a flat list. We're not paying attention to where
1020-
// these methods are actually coming from. This might turn out to be a
1021-
// problem if we want to show that info later.
1022-
List<Field> _propertiesForPages;
1023-
List<Field> get propertiesForPages {
1024-
if (_propertiesForPages == null) {
1025-
_propertiesForPages = []
1026-
..addAll(allInstanceProperties)
1027-
..sort(byName);
1028-
}
1029-
return _propertiesForPages;
1030-
}
1031-
1032961
List<Method> get staticMethods {
1033962
if (_staticMethods != null) return _staticMethods;
1034963

@@ -1042,9 +971,8 @@ class Class extends ModelElement
1042971

1043972
List<Field> get staticProperties {
1044973
if (_staticFields != null) return _staticFields;
1045-
_staticFields = allFields
1046-
.where((f) => f.isStatic)
1047-
.where((f) => !f.isConst)
974+
_staticFields = _allFields
975+
.where((f) => f.isStatic && !f.isConst)
1048976
.toList(growable: false)
1049977
..sort(byName);
1050978

@@ -1124,7 +1052,9 @@ class Class extends ModelElement
11241052
return __inheritedElements;
11251053
}
11261054

1127-
List<Field> get allFields {
1055+
/// Internal only because subclasses are allowed to override how
1056+
/// these are mapped to [allInheritedFields] and so forth.
1057+
List<Field> get _allFields {
11281058
if (_fields != null) return _fields;
11291059
_fields = [];
11301060
Set<PropertyAccessorElement> inheritedAccessors = new Set()
@@ -1243,12 +1173,18 @@ class Class extends ModelElement
12431173
return _allMethods;
12441174
}
12451175

1176+
List<TypeParameter> _typeParameters;
12461177
// a stronger hash?
12471178
@override
1248-
List<TypeParameter> get typeParameters => _cls.typeParameters.map((f) {
1179+
List<TypeParameter> get typeParameters {
1180+
if (_typeParameters == null) {
1181+
_typeParameters = _cls.typeParameters.map((f) {
12491182
var lib = new Library(f.enclosingElement.library, packageGraph);
12501183
return new ModelElement.from(f, lib, packageGraph) as TypeParameter;
12511184
}).toList();
1185+
}
1186+
return _typeParameters;
1187+
}
12521188

12531189
@override
12541190
bool operator ==(o) =>
@@ -1594,9 +1530,6 @@ class Enum extends Class {
15941530
return _instanceProperties;
15951531
}
15961532

1597-
@override
1598-
List<Field> get propertiesForPages => allInstanceProperties;
1599-
16001533
@override
16011534
String get kind => 'enum';
16021535
}
@@ -1859,6 +1792,12 @@ class Field extends ModelElement
18591792
abstract class GetterSetterCombo implements ModelElement {
18601793
Accessor get getter;
18611794

1795+
Iterable<Accessor> get allAccessors sync* {
1796+
for (Accessor a in [getter, setter]) {
1797+
if (a != null) yield a;
1798+
}
1799+
}
1800+
18621801
Set<String> get comboFeatures {
18631802
Set<String> allFeatures = new Set();
18641803
if (hasExplicitGetter && hasPublicGetter)

0 commit comments

Comments
 (0)