Joal has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/311127

Change subject: Add singleton capactiy to UAParser
......................................................................

Add singleton capactiy to UAParser

Being a singleton is the easiest way for UAParser to be usable by Spark.
Others involves serialization issues that are way harder to solve.

Bug: T121550
Change-Id: I7c1f2711bc56fb3a12f15c13b08840556ba03139
---
M 
refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/UAParser.java
1 file changed, 19 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/analytics/refinery/source 
refs/changes/27/311127/1

diff --git 
a/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/UAParser.java
 
b/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/UAParser.java
index 1ea7707..a26eb6b 100644
--- 
a/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/UAParser.java
+++ 
b/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/UAParser.java
@@ -31,10 +31,24 @@
 
     public static final String NA = "-";
 
-    static final Logger LOG = Logger.getLogger(UAParser.class.getName());
+    private static final Logger LOG = 
Logger.getLogger(UAParser.class.getName());
 
-    private CachingParser cachingParser;
-    private Map<String, String> result = new HashMap<String, String>();
+    private static CachingParser cachingParser;
+
+    /*
+     * Meta-methods to enable eager instantiation in a singleton-based way.
+     * in non-Java terms: you get to only create one class instance, and only
+     * when you need it, instead of always having everything (static/eager 
instantiation)
+     * or always generating everything anew (!singletons). So we have:
+     * (1) an instance;
+     * (2) an empty constructor (to avoid people just calling the constructor);
+     * (3) an actual getInstance method to allow for instantiation.
+     */
+    private static final UAParser instance = new UAParser();
+
+    public static UAParser getInstance(){
+        return instance;
+    }
 
     /**
      * Function replacing null/empty string with the NA one.
@@ -70,7 +84,8 @@
      * os_family, os_major, os_minor, wmf_app_version keys and associated 
values.
      */
     public Map<String, String> getUAMap(String uaString) {
-        result.clear();
+        // Presetting map size to correct number of slots
+        Map<String, String> result = new HashMap<>(8);
 
         UserAgent browser = null;
         Device device = null;

-- 
To view, visit https://gerrit.wikimedia.org/r/311127
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7c1f2711bc56fb3a12f15c13b08840556ba03139
Gerrit-PatchSet: 1
Gerrit-Project: analytics/refinery/source
Gerrit-Branch: master
Gerrit-Owner: Joal <j...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to