From 3f3471846509116db6b719619bc71fb13b796ba6 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 4 Mar 2015 08:27:04 -0800 Subject: [PATCH] Clean up some Dart idioms in fn This CL adds license blocks and cleans up a number of Dart idioms in fn. Specifically, I've marked several fields as |final| and used Map#putIfAbsent in some appropriate places. R=rafaelw@chromium.org Review URL: https://codereview.chromium.org/973613004 --- examples/fn/lib/component.dart | 6 +++++- examples/fn/lib/fn.dart | 6 +++++- examples/fn/lib/node.dart | 26 +++++++++++++++----------- examples/fn/lib/reflect.dart | 16 +++++++++------- examples/fn/lib/style.dart | 24 ++++++++++-------------- 5 files changed, 44 insertions(+), 34 deletions(-) diff --git a/examples/fn/lib/component.dart b/examples/fn/lib/component.dart index 1263b41625..1503e6348c 100644 --- a/examples/fn/lib/component.dart +++ b/examples/fn/lib/component.dart @@ -1,3 +1,7 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + part of fn; List _dirtyComponents = new List(); @@ -30,7 +34,7 @@ abstract class Component extends Node { bool _dirty = true; // components begin dirty because they haven't rendered. Node _rendered = null; bool _removed = false; - int _order; + final int _order; static int _currentOrder = 0; bool _stateful; static Component _currentlyRendering; diff --git a/examples/fn/lib/fn.dart b/examples/fn/lib/fn.dart index 30ec0408a6..ab67c50bd6 100644 --- a/examples/fn/lib/fn.dart +++ b/examples/fn/lib/fn.dart @@ -1,3 +1,7 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + library fn; import 'dart:async'; @@ -11,7 +15,7 @@ part 'style.dart'; bool _checkedMode; -bool debugWarnings() { +bool _debugWarnings() { void testFn(double i) {} if (_checkedMode == null) { diff --git a/examples/fn/lib/node.dart b/examples/fn/lib/node.dart index f7ea1a3014..1339e204ea 100644 --- a/examples/fn/lib/node.dart +++ b/examples/fn/lib/node.dart @@ -1,8 +1,12 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + part of fn; -void parentInsertBefore(sky.ParentNode parent, - sky.Node node, - sky.Node ref) { +void _parentInsertBefore(sky.ParentNode parent, + sky.Node node, + sky.Node ref) { if (ref != null) { ref.insertBefore([node]); } else { @@ -41,7 +45,7 @@ class Text extends Node { bool _sync(Node old, sky.ParentNode host, sky.Node insertBefore) { if (old == null) { _root = new sky.Text(data); - parentInsertBefore(host, _root, insertBefore); + _parentInsertBefore(host, _root, insertBefore); return false; } @@ -51,7 +55,7 @@ class Text extends Node { } } -var _emptyList = new List(); +final List _emptyList = new List(); abstract class Element extends Node { @@ -102,7 +106,7 @@ abstract class Element extends Node { _className = style == null ? '': style._className; _children = children == null ? _emptyList : children; - if (debugWarnings()) { + if (_debugWarnings()) { _debugReportDuplicateIds(); } } @@ -194,7 +198,7 @@ abstract class Element extends Node { assert(child._root is sky.Node); } - parentInsertBefore(host, _root, insertBefore); + _parentInsertBefore(host, _root, insertBefore); return false; } @@ -283,7 +287,7 @@ abstract class Element extends Node { oldNodeIdMap[currentNode._key] = null; // mark it reordered. // print("Reparenting ${currentNode._key}"); - parentInsertBefore(root, oldNode._root, nextSibling); + _parentInsertBefore(root, oldNode._root, nextSibling); return true; } @@ -344,7 +348,7 @@ class Container extends Element { String get _tagName => 'div'; - static Container _emptyContainer = new Container(); + static final Container _emptyContainer = new Container(); Element get _emptyElement => _emptyContainer; @@ -389,7 +393,7 @@ class Image extends Element { String get _tagName => 'img'; - static Image _emptyImage = new Image(); + static final Image _emptyImage = new Image(); Element get _emptyElement => _emptyImage; String src; @@ -457,7 +461,7 @@ class Anchor extends Element { String get _tagName => 'a'; - static Anchor _emptyAnchor = new Anchor(); + static final Anchor _emptyAnchor = new Anchor(); String href; diff --git a/examples/fn/lib/reflect.dart b/examples/fn/lib/reflect.dart index 66b2efefc2..99dcaa7c39 100644 --- a/examples/fn/lib/reflect.dart +++ b/examples/fn/lib/reflect.dart @@ -1,15 +1,17 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + library reflect; import 'dart:mirrors'; import 'dart:collection'; -HashMap _fieldCache = new HashMap(); +final HashMap _fieldCache = new HashMap(); List _getPublicFields(ClassMirror mirror) { - var fields = _fieldCache[mirror]; - if (fields == null) { - fields = new List(); - _fieldCache[mirror] = fields; + return _fieldCache.putIfAbsent(mirror, () { + List fields = new List(); while (mirror != null) { var decls = mirror.declarations; @@ -25,9 +27,9 @@ List _getPublicFields(ClassMirror mirror) { mirror = mirror.superclass; } - } - return fields; + return fields; + }); } void copyPublicFields(Object source, Object target) { diff --git a/examples/fn/lib/style.dart b/examples/fn/lib/style.dart index c89db5e73d..4dd510f728 100644 --- a/examples/fn/lib/style.dart +++ b/examples/fn/lib/style.dart @@ -1,17 +1,21 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + part of fn; class Style { final String _className; - static Map _cache = null; + static final Map _cache = new HashMap(); static int nextStyleId = 1; static String nextClassName(String styles) { assert(sky.document != null); - var className = "style$nextStyleId"; + String className = "style$nextStyleId"; nextStyleId++; - var styleNode = sky.document.createElement('style'); + sky.Element styleNode = sky.document.createElement('style'); styleNode.setChild(new sky.Text(".$className { $styles }")); sky.document.appendChild(styleNode); @@ -19,17 +23,9 @@ class Style { } factory Style(String styles) { - if (_cache == null) { - _cache = new HashMap(); - } - - var style = _cache[styles]; - if (style == null) { - style = new Style._internal(nextClassName(styles)); - _cache[styles] = style; - } - - return style; + return _cache.putIfAbsent(styles, () { + return new Style._internal(nextClassName(styles)); + }); } Style._internal(this._className);