dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #14114
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4667: Impl class WeightedPaginatedList
------------------------------------------------------------
revno: 4667
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2011-09-23 21:39:08 +0200
message:
Impl class WeightedPaginatedList
added:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Weighted.java
dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/WeightedPaginatedList.java
dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/WeightedPaginatedListTest.java
modified:
dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PaginatedList.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Weighted.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Weighted.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Weighted.java 2011-09-23 19:39:08 +0000
@@ -0,0 +1,36 @@
+package org.hisp.dhis.common;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @author Lars Helge Overland
+ */
+public interface Weighted
+{
+ int getWeight();
+}
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PaginatedList.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PaginatedList.java 2011-07-01 07:12:30 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PaginatedList.java 2011-09-23 19:39:08 +0000
@@ -48,7 +48,7 @@
private int fromIndex = 0;
- public PaginatedList( Collection<? extends T> collection )
+ public PaginatedList( Collection<T> collection )
{
super( collection );
this.pageSize = DEFAULT_PAGE_SIZE;
=== added file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/WeightedPaginatedList.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/WeightedPaginatedList.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/WeightedPaginatedList.java 2011-09-23 19:39:08 +0000
@@ -0,0 +1,116 @@
+package org.hisp.dhis.system.util;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import org.hisp.dhis.common.Weighted;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class WeightedPaginatedList<T extends Weighted>
+ extends ArrayList<T>
+{
+ private int pages = 0;
+ private int totalWeight = 0;
+ private int weightPageBreak = 0;
+ private int startIndex = 0;
+
+ public WeightedPaginatedList( Collection<T> collection, int pages )
+ {
+ super( collection );
+ this.pages = pages;
+ this.init();
+ }
+
+ private void init()
+ {
+ Iterator<T> iterator = super.iterator();
+
+ while ( iterator.hasNext() )
+ {
+ T element = iterator.next();
+
+ totalWeight += element != null ? element.getWeight() : 0;
+ }
+
+ weightPageBreak = (int) Math.ceil( (double) totalWeight / pages );
+
+ System.out.println( "tot " + totalWeight + " break " + weightPageBreak );
+ }
+
+ /**
+ * Returns the next page in the list. Returns null if there are no more pages.
+ */
+ public List<T> nextPage()
+ {
+ int size = size();
+
+ if ( startIndex >= size )
+ {
+ return null;
+ }
+
+ int currentWeight = 0;
+ int currentIndex = startIndex;
+
+ while ( currentWeight < weightPageBreak && currentIndex < size )
+ {
+ T element = get( currentIndex++ );
+
+ currentWeight += element != null ? element.getWeight() : 0;
+ }
+
+ List<T> page = super.subList( startIndex, currentIndex );
+
+ startIndex = currentIndex;
+
+ return page;
+ }
+
+ /**
+ * Returns a list of all pages.
+ */
+ public List<List<T>> getPages()
+ {
+ List<List<T>> pages = new ArrayList<List<T>>();
+
+ List<T> page = new ArrayList<T>();
+
+ while ( ( page = nextPage() ) != null )
+ {
+ pages.add( page );
+ }
+
+ return pages;
+ }
+}
=== added file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/WeightedPaginatedListTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/WeightedPaginatedListTest.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/WeightedPaginatedListTest.java 2011-09-23 19:39:08 +0000
@@ -0,0 +1,168 @@
+package org.hisp.dhis.system.util;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.hisp.dhis.common.Weighted;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class WeightedPaginatedListTest
+{
+ private Weighted one = new One();
+ private Weighted two = new Two();
+ private Weighted three = new Three();
+
+ // -------------------------------------------------------------------------
+ // Tests
+ // -------------------------------------------------------------------------
+
+ @Test
+ public void testNextPageA()
+ {
+ WeightedPaginatedList<Weighted> list = new WeightedPaginatedList<Weighted>(
+ Arrays.asList( one, one, three, three, three, one, one, one ), 3 );
+
+ List<Weighted> page = list.nextPage();
+
+ assertNotNull( page );
+ assertEquals( 3, page.size() );
+ assertTrue( page.contains( one ) );
+ assertTrue( page.contains( three ) );
+
+ page = list.nextPage();
+
+ assertNotNull( page );
+ assertEquals( 2, page.size() );
+ assertTrue( page.contains( three ) );
+
+ page = list.nextPage();
+
+ assertNotNull( page );
+ assertEquals( 3, page.size() );
+ assertTrue( page.contains( one ) );
+ }
+
+ @Test
+ public void testNextPageB()
+ {
+ WeightedPaginatedList<Weighted> list = new WeightedPaginatedList<Weighted>(
+ Arrays.asList( one, two, three, two, three, one, one, two, three, one ), 4 );
+
+ List<Weighted> page = list.nextPage();
+
+ assertNotNull( page );
+ assertEquals( 3, page.size() );
+ assertTrue( page.contains( one ) );
+ assertTrue( page.contains( two ) );
+ assertTrue( page.contains( three ) );
+
+ page = list.nextPage();
+
+ assertNotNull( page );
+ assertEquals( 2, page.size() );
+ assertTrue( page.contains( two ) );
+ assertTrue( page.contains( three ) );
+
+ page = list.nextPage();
+
+ assertNotNull( page );
+ assertEquals( 4, page.size() );
+ assertTrue( page.contains( one ) );
+ assertTrue( page.contains( two ) );
+ assertTrue( page.contains( three ) );
+
+ page = list.nextPage();
+
+ assertNotNull( page );
+ assertEquals( 1, page.size() );
+ assertTrue( page.contains( one ) );
+ }
+
+ @Test
+ public void testGetPages()
+ {
+ WeightedPaginatedList<Weighted> list = new WeightedPaginatedList<Weighted>(
+ Arrays.asList( three, three, one, one, one, one, two, two ), 3 );
+
+ List<List<Weighted>> pages = list.getPages();
+
+ List<Weighted> page = pages.get( 0 );
+
+ assertNotNull( page );
+ assertEquals( 2, page.size() );
+ assertTrue( page.contains( three ) );
+
+ page = pages.get( 1 );
+
+ assertNotNull( page );
+ assertEquals( 5, page.size() );
+ assertTrue( page.contains( one ) );
+ assertTrue( page.contains( two ) );
+
+ page = pages.get( 2 );
+
+ assertNotNull( page );
+ assertEquals( 1, page.size() );
+ assertTrue( page.contains( two ) );
+ }
+
+ // -------------------------------------------------------------------------
+ // Test support classes
+ // -------------------------------------------------------------------------
+
+ class One implements Weighted
+ {
+ public int getWeight()
+ {
+ return 1;
+ }
+ }
+
+ class Two implements Weighted
+ {
+ public int getWeight()
+ {
+ return 2;
+ }
+ }
+
+ class Three implements Weighted
+ {
+ public int getWeight()
+ {
+ return 3;
+ }
+ }
+}