From 22e1159736bc9d32a879e20292bbda98428c1d7d Mon Sep 17 00:00:00 2001 From: tsauerwein Date: Fri, 31 Oct 2014 11:18:17 +0100 Subject: [PATCH] Implement hashCode for ol.style.Circle --- src/ol/style/circlestyle.js | 20 +++++ src/ol/style/fillstyle.js | 18 ++++ src/ol/style/strokestyle.js | 28 ++++++ test/spec/ol/style/circlestyle.test.js | 117 +++++++++++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 test/spec/ol/style/circlestyle.test.js diff --git a/src/ol/style/circlestyle.js b/src/ol/style/circlestyle.js index 0d209ba876..235d066290 100644 --- a/src/ol/style/circlestyle.js +++ b/src/ol/style/circlestyle.js @@ -2,8 +2,10 @@ goog.provide('ol.style.Circle'); goog.require('goog.dom'); goog.require('goog.dom.TagName'); +goog.require('goog.string'); goog.require('ol.color'); goog.require('ol.render.canvas'); +goog.require('ol.structs.IHashable'); goog.require('ol.style.Fill'); goog.require('ol.style.Image'); goog.require('ol.style.ImageState'); @@ -18,6 +20,7 @@ goog.require('ol.style.Stroke'); * @constructor * @param {olx.style.CircleOptions=} opt_options Options. * @extends {ol.style.Image} + * @implements {ol.structs.IHashable} * @api */ ol.style.Circle = function(opt_options) { @@ -260,3 +263,20 @@ ol.style.Circle.prototype.render_ = function() { return size; }; + + +/** + * @inheritDoc + */ +ol.style.Circle.prototype.hashCode = function() { + var hash = 17; + + hash = hash * 23 + (!goog.isNull(this.stroke_) ? + this.stroke_.hashCode() : 0); + hash = hash * 23 + (!goog.isNull(this.fill_) ? + this.fill_.hashCode() : 0); + hash = hash * 23 + (goog.isDef(this.radius_) ? + goog.string.hashCode(this.radius_.toString()) : 0); + + return hash; +}; diff --git a/src/ol/style/fillstyle.js b/src/ol/style/fillstyle.js index 6714445868..65091029a7 100644 --- a/src/ol/style/fillstyle.js +++ b/src/ol/style/fillstyle.js @@ -1,5 +1,9 @@ goog.provide('ol.style.Fill'); +goog.require('goog.string'); +goog.require('ol.color'); +goog.require('ol.structs.IHashable'); + /** @@ -8,6 +12,7 @@ goog.provide('ol.style.Fill'); * * @constructor * @param {olx.style.FillOptions=} opt_options Options. + * @implements {ol.structs.IHashable} * @api */ ol.style.Fill = function(opt_options) { @@ -40,3 +45,16 @@ ol.style.Fill.prototype.getColor = function() { ol.style.Fill.prototype.setColor = function(color) { this.color_ = color; }; + + +/** + * @inheritDoc + */ +ol.style.Fill.prototype.hashCode = function() { + var hash = 17; + + hash = hash * 23 + (!goog.isNull(this.color_) ? + goog.string.hashCode(ol.color.asString(this.color_)) : 0); + + return hash; +}; diff --git a/src/ol/style/strokestyle.js b/src/ol/style/strokestyle.js index 15e99bf260..934490f3b1 100644 --- a/src/ol/style/strokestyle.js +++ b/src/ol/style/strokestyle.js @@ -1,5 +1,9 @@ goog.provide('ol.style.Stroke'); +goog.require('goog.string'); +goog.require('ol.color'); +goog.require('ol.structs.IHashable'); + /** @@ -11,6 +15,7 @@ goog.provide('ol.style.Stroke'); * * @constructor * @param {olx.style.StrokeOptions=} opt_options Options. + * @implements {ol.structs.IHashable} * @api */ ol.style.Stroke = function(opt_options) { @@ -173,3 +178,26 @@ ol.style.Stroke.prototype.setMiterLimit = function(miterLimit) { ol.style.Stroke.prototype.setWidth = function(width) { this.width_ = width; }; + + +/** + * @inheritDoc + */ +ol.style.Stroke.prototype.hashCode = function() { + var hash = 17; + + hash = hash * 23 + (!goog.isNull(this.color_) ? + goog.string.hashCode(ol.color.asString(this.color_)) : 0); + hash = hash * 23 + (goog.isDef(this.lineCap_) ? + goog.string.hashCode(this.lineCap_.toString()) : 0); + hash = hash * 23 + (!goog.isNull(this.lineDash_) ? + goog.string.hashCode(this.lineDash_.toString()) : 0); + hash = hash * 23 + (goog.isDef(this.lineJoin_) ? + goog.string.hashCode(this.lineJoin_) : 0); + hash = hash * 23 + (goog.isDef(this.miterLimit_) ? + goog.string.hashCode(this.miterLimit_.toString()) : 0); + hash = hash * 23 + (goog.isDef(this.width_) ? + goog.string.hashCode(this.width_.toString()) : 0); + + return hash; +}; diff --git a/test/spec/ol/style/circlestyle.test.js b/test/spec/ol/style/circlestyle.test.js new file mode 100644 index 0000000000..566164e199 --- /dev/null +++ b/test/spec/ol/style/circlestyle.test.js @@ -0,0 +1,117 @@ +goog.provide('ol.test.style.Circle'); + + +describe('ol.style.Circle', function() { + + describe('#hashCode', function() { + + it('calculates the same hash code for default options', function() { + var style1 = new ol.style.Circle(); + var style2 = new ol.style.Circle(); + expect(style1.hashCode()).to.eql(style2.hashCode()); + }); + + it('calculates not the same hash code (radius)', function() { + var style1 = new ol.style.Circle(); + var style2 = new ol.style.Circle({ + radius: 5 + }); + expect(style1.hashCode()).to.not.eql(style2.hashCode()); + }); + + it('calculates the same hash code (radius)', function() { + var style1 = new ol.style.Circle({ + radius: 5 + }); + var style2 = new ol.style.Circle({ + radius: 5 + }); + expect(style1.hashCode()).to.eql(style2.hashCode()); + }); + + it('calculates not the same hash code (color)', function() { + var style1 = new ol.style.Circle({ + radius: 5, + fill: new ol.style.Fill({ + color: '#319FD3' + }) + }); + var style2 = new ol.style.Circle({ + radius: 5, + stroke: new ol.style.Stroke({ + color: '#319FD3' + }) + }); + expect(style1.hashCode()).to.not.eql(style2.hashCode()); + }); + + it('calculates the same hash code (everything set)', function() { + var style1 = new ol.style.Circle({ + radius: 5, + fill: new ol.style.Fill({ + color: '#319FD3' + }), + stroke: new ol.style.Stroke({ + color: '#319FD3', + lineCap: 'round', + lineDash: [5, 15, 25], + lineJoin: 'miter', + miterLimit: 4, + width: 2 + }) + }); + var style2 = new ol.style.Circle({ + radius: 5, + fill: new ol.style.Fill({ + color: '#319FD3' + }), + stroke: new ol.style.Stroke({ + color: '#319FD3', + lineCap: 'round', + lineDash: [5, 15, 25], + lineJoin: 'miter', + miterLimit: 4, + width: 2 + }) + }); + expect(style1.hashCode()).to.eql(style2.hashCode()); + }); + + it('calculates not the same hash code (stroke width differs)', function() { + var style1 = new ol.style.Circle({ + radius: 5, + fill: new ol.style.Fill({ + color: '#319FD3' + }), + stroke: new ol.style.Stroke({ + color: '#319FD3', + lineCap: 'round', + lineDash: [5, 15, 25], + lineJoin: 'miter', + miterLimit: 4, + width: 3 + }) + }); + var style2 = new ol.style.Circle({ + radius: 5, + fill: new ol.style.Fill({ + color: '#319FD3' + }), + stroke: new ol.style.Stroke({ + color: '#319FD3', + lineCap: 'round', + lineDash: [5, 15, 25], + lineJoin: 'miter', + miterLimit: 4, + width: 2 + }) + }); + expect(style1.hashCode()).to.not.eql(style2.hashCode()); + }); + + }); +}); + +goog.require('ol.style.Circle'); +goog.require('ol.style.Fill'); +goog.require('ol.style.Stroke');