Mercurial > hg > release > thermostat-0.5
changeset 867:b9fb5202738f
Move LongRange and LongRangeNormalizer to common
review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-December/004782.html
reviewed-by: omajid
author | Mario Torre <neugens.limasoftware@gmail.com> |
---|---|
date | Mon, 17 Dec 2012 20:41:17 +0100 |
parents | d999dac7925c |
children | 0926cdd87a6b |
files | client/swing/src/main/java/com/redhat/thermostat/client/swing/components/models/LongRange.java client/swing/src/main/java/com/redhat/thermostat/client/swing/components/models/LongRangeNormalizer.java client/swing/src/test/java/com/redhat/thermostat/client/swing/components/models/LongRangeNormalizerTest.java common/core/pom.xml common/core/src/main/java/com/redhat/thermostat/common/model/LongRange.java common/core/src/main/java/com/redhat/thermostat/common/model/LongRangeNormalizer.java common/core/src/test/java/com/redhat/thermostat/common/model/LongRangeNormalizerTest.java thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/SwingThreadTimelineChart.java |
diffstat | 8 files changed, 311 insertions(+), 263 deletions(-) [+] |
line wrap: on
line diff
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/models/LongRange.java Mon Dec 17 20:40:52 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +0,0 @@ -/* - * Copyright 2012 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.client.swing.components.models; - -public class LongRange { - - long min; - long max; - - public void setMax(long max) { - this.max = max; - } - - public long getMax() { - return max; - } - - public void setMin(long min) { - this.min = min; - } - - - public long getMin() { - return min; - } -}
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/models/LongRangeNormalizer.java Mon Dec 17 20:40:52 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,81 +0,0 @@ -/* - * Copyright 2012 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.client.swing.components.models; - -public class LongRangeNormalizer { - - private long minNormalized; - - private long maxNormalized; - - private long value; - - private LongRange range; - - public LongRangeNormalizer(LongRange range) { - this.range = range; - } - - public void setMaxNormalized(long maxNormalized) { - this.maxNormalized = maxNormalized; - } - - public void setMinNormalized(long minNormalized) { - this.minNormalized = minNormalized; - } - - public long getValue() { - return value; - } - - public void setValue(long newValue) { - this.value = newValue; - } - - public long getMaxNormalized() { - return maxNormalized; - } - - public long getMinNormalized() { - return minNormalized; - } - - public long getValueNormalized() { - double normalized = ((value - range.min) * (double)(maxNormalized - minNormalized)/(range.max - range.min)) + minNormalized; - return Math.round(normalized); - } -}
--- a/client/swing/src/test/java/com/redhat/thermostat/client/swing/components/models/LongRangeNormalizerTest.java Mon Dec 17 20:40:52 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,120 +0,0 @@ -/* - * Copyright 2012 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.client.swing.components.models; - -import static org.junit.Assert.*; -import junit.framework.Assert; - -import org.junit.Test; - -public class LongRangeNormalizerTest { - - @Test - public void testSameRange() { - - LongRange range = new LongRange(); - range.setMax(10); - range.setMin(0); - - LongRangeNormalizer model = new LongRangeNormalizer(range); - - model.setValue(5); - - model.setMaxNormalized(10); - model.setMinNormalized(0); - - - Assert.assertEquals((int) model.getValue(), model.getValueNormalized()); - } - - @Test - public void testDoubleRange() { - - LongRange range = new LongRange(); - range.setMax(10); - range.setMin(0); - - LongRangeNormalizer model = new LongRangeNormalizer(range); - - model.setValue(5); - - model.setMaxNormalized(20); - model.setMinNormalized(0); - - - Assert.assertEquals(10, model.getValueNormalized()); - } - - @Test - public void testRanges() { - - LongRange range = new LongRange(); - range.setMax(10); - range.setMin(0); - - LongRangeNormalizer model = new LongRangeNormalizer(range); - - model.setValue(5); - - model.setMaxNormalized(40); - model.setMinNormalized(0); - - Assert.assertEquals(20, model.getValueNormalized()); - - model.setMaxNormalized(60); - model.setMinNormalized(0); - - Assert.assertEquals(30, model.getValueNormalized()); - - model.setMaxNormalized(200); - model.setMinNormalized(100); - - Assert.assertEquals(150, model.getValueNormalized()); - - range.setMax(100); - range.setMin(0); - model.setValue(50); - - model.setMaxNormalized(1); - model.setMinNormalized(0); - - Assert.assertEquals(1, model.getValueNormalized()); - - model.setValue(49); - Assert.assertEquals(0, model.getValueNormalized()); - } -}
--- a/common/core/pom.xml Mon Dec 17 20:40:52 2012 +0100 +++ b/common/core/pom.xml Mon Dec 17 20:41:17 2012 +0100 @@ -89,6 +89,7 @@ com.redhat.thermostat.common.heap, com.redhat.thermostat.common.locale, com.redhat.thermostat.common.ssl, + com.redhat.thermostat.common.model, </Export-Package> <Private-Package> com.redhat.thermostat.test,
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/core/src/main/java/com/redhat/thermostat/common/model/LongRange.java Mon Dec 17 20:41:17 2012 +0100 @@ -0,0 +1,85 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.common.model; + + +/** + * A class representing a span of units from min to max. <br /><br /> + * + * The units and the meaning of the range are left to the user, including the + * fact that the range are inclusive or exclusive of the extremes. + */ +public class LongRange { + + long min; + long max; + + /** + * Creates a new LongRange with a span of 0 units. + */ + public LongRange() { + } + + /** + * Creates a new Range that span from min to max. + */ + public LongRange(long min, long max) { + this.min = min; + this.max = max; + } + + public void setMax(long max) { + this.max = max; + } + + public long getMax() { + return max; + } + + public void setMin(long min) { + this.min = min; + } + + public long getMin() { + return min; + } + + @Override + public String toString() { + return "[" + min + " ->" + max + "("+ (max - min) + ")]"; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/core/src/main/java/com/redhat/thermostat/common/model/LongRangeNormalizer.java Mon Dec 17 20:41:17 2012 +0100 @@ -0,0 +1,100 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.common.model; + +/** + * A class that normalizes {@link LongRange} values to another given range. + * + * <br /><br /> + * + * The range of normalization is inclusive of the extremes. + */ +public class LongRangeNormalizer { + + private long minNormalized; + + private long maxNormalized; + + private long value; + + private LongRange range; + + public LongRangeNormalizer(LongRange range) { + this.range = range; + } + + public LongRangeNormalizer(LongRange range, long minNormalized, long maxNormalized) { + this.range = range; + this.maxNormalized = maxNormalized; + this.minNormalized = minNormalized; + } + + public LongRangeNormalizer(LongRange range, LongRange normilizedRange) { + this.range = range; + maxNormalized = normilizedRange.max; + minNormalized = normilizedRange.min; + } + + public void setMaxNormalized(long maxNormalized) { + this.maxNormalized = maxNormalized; + } + + public void setMinNormalized(long minNormalized) { + this.minNormalized = minNormalized; + } + + public long getValue() { + return value; + } + + public void setValue(long newValue) { + this.value = newValue; + } + + public long getMaxNormalized() { + return maxNormalized; + } + + public long getMinNormalized() { + return minNormalized; + } + + public long getValueNormalized() { + double normalized = ((value - range.min) * (double)(maxNormalized - minNormalized)/(range.max - range.min)) + minNormalized; + return Math.round(normalized); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/core/src/test/java/com/redhat/thermostat/common/model/LongRangeNormalizerTest.java Mon Dec 17 20:41:17 2012 +0100 @@ -0,0 +1,123 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.common.model; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Test; + +import com.redhat.thermostat.common.model.LongRange; +import com.redhat.thermostat.common.model.LongRangeNormalizer; + +public class LongRangeNormalizerTest { + + @Test + public void testSameRange() { + + LongRange range = new LongRange(); + range.setMax(10); + range.setMin(0); + + LongRangeNormalizer model = new LongRangeNormalizer(range); + + model.setValue(5); + + model.setMaxNormalized(10); + model.setMinNormalized(0); + + + Assert.assertEquals((int) model.getValue(), model.getValueNormalized()); + } + + @Test + public void testDoubleRange() { + + LongRange range = new LongRange(); + range.setMax(10); + range.setMin(0); + + LongRangeNormalizer model = new LongRangeNormalizer(range); + + model.setValue(5); + + model.setMaxNormalized(20); + model.setMinNormalized(0); + + + Assert.assertEquals(10, model.getValueNormalized()); + } + + @Test + public void testRanges() { + + LongRange range = new LongRange(); + range.setMax(10); + range.setMin(0); + + LongRangeNormalizer model = new LongRangeNormalizer(range); + + model.setValue(5); + + model.setMaxNormalized(40); + model.setMinNormalized(0); + + Assert.assertEquals(20, model.getValueNormalized()); + + model.setMaxNormalized(60); + model.setMinNormalized(0); + + Assert.assertEquals(30, model.getValueNormalized()); + + model.setMaxNormalized(200); + model.setMinNormalized(100); + + Assert.assertEquals(150, model.getValueNormalized()); + + range.setMax(100); + range.setMin(0); + model.setValue(50); + + model.setMaxNormalized(1); + model.setMinNormalized(0); + + Assert.assertEquals(1, model.getValueNormalized()); + + model.setValue(49); + Assert.assertEquals(0, model.getValueNormalized()); + } +}
--- a/thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/SwingThreadTimelineChart.java Mon Dec 17 20:40:52 2012 +0100 +++ b/thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/SwingThreadTimelineChart.java Mon Dec 17 20:41:17 2012 +0100 @@ -59,9 +59,9 @@ import com.redhat.thermostat.client.swing.components.GradientRoundBorder; import com.redhat.thermostat.client.swing.components.GraphicsUtils; -import com.redhat.thermostat.client.swing.components.models.LongRange; -import com.redhat.thermostat.client.swing.components.models.LongRangeNormalizer; import com.redhat.thermostat.client.ui.Palette; +import com.redhat.thermostat.common.model.LongRange; +import com.redhat.thermostat.common.model.LongRangeNormalizer; import com.redhat.thermostat.thread.client.common.ThreadTimelineBean; import com.redhat.thermostat.thread.client.common.chart.ChartColors;