[gatein-commits] gatein SVN: r1340 - in portal/trunk/component/common/src: test/java/org/exoplatform/commons/utils and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Sun Jan 17 19:14:27 EST 2010


Author: julien_viet
Date: 2010-01-17 19:14:27 -0500 (Sun, 17 Jan 2010)
New Revision: 1340

Added:
   portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/NoArgConstructorPageList.java
   portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/PageListAccess.java
   portal/trunk/component/common/src/test/java/org/exoplatform/commons/utils/StringPageListAccess.java
   portal/trunk/component/common/src/test/java/org/exoplatform/commons/utils/TestPageListAccess.java
Log:
version of PageList that can "reconnect" to a data set

Added: portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/NoArgConstructorPageList.java
===================================================================
--- portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/NoArgConstructorPageList.java	                        (rev 0)
+++ portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/NoArgConstructorPageList.java	2010-01-18 00:14:27 UTC (rev 1340)
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.commons.utils;
+
+/**
+ * This class is required for the {@link org.exoplatform.commons.utils.PageListAccess} class to be serializable.
+ *
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public abstract class NoArgConstructorPageList<E> extends PageList<E>
+{
+
+   protected NoArgConstructorPageList(int pageSize)
+   {
+      super(pageSize);
+   }
+
+   protected NoArgConstructorPageList()
+   {
+      super(10);
+   }
+}

Added: portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/PageListAccess.java
===================================================================
--- portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/PageListAccess.java	                        (rev 0)
+++ portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/PageListAccess.java	2010-01-18 00:14:27 UTC (rev 1340)
@@ -0,0 +1,234 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.commons.utils;
+
+import java.io.*;
+import java.lang.reflect.Field;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public abstract class PageListAccess<E, S extends Serializable> extends NoArgConstructorPageList<E> implements Serializable
+{
+
+   /** . */
+   private static final Field pageSizeField;
+
+   static
+   {
+      try
+      {
+         pageSizeField = PageList.class.getDeclaredField("pageSize_");
+         pageSizeField.setAccessible(true);
+      }
+      catch (NoSuchFieldException e)
+      {
+         throw new Error(e);
+      }
+   }
+
+   /** The state that recreates the list. */
+   private S state;
+
+   /** . */
+   private LazyList<E> lazyList;
+
+   protected PageListAccess(S state, int pageSize)
+   {
+      super(pageSize);
+
+      //
+      if (state == null)
+      {
+         throw new NullPointerException();
+      }
+      if (pageSize < 0)
+      {
+         throw new IllegalArgumentException();
+      }
+
+      //
+      this.state = state;
+      this.lazyList = null;
+   }
+
+   private void ensureCorrectState()
+   {
+      if (lazyList == null)
+      {
+         lazyList = new LazyList<E>(create(state), super.getPageSize());
+
+         // Save temporarily the current page
+         int currentPage = currentPage_;
+
+         // Refresh state
+         super.setAvailablePage(lazyList.size());
+
+         // Put back current page that was written by previous method call
+         if (currentPage != -1)
+         {
+            currentPage_ = currentPage;
+         }
+      }
+   }
+
+
+   @Override
+   protected final void populateCurrentPage(int page) throws Exception
+   {
+      // Make sure we have correct state
+      ensureCorrectState();
+
+      //
+      int from = getFrom();
+      int to = getTo();
+      currentListPage_ = lazyList.subList(from, to);
+   }
+
+   @Override
+   public final List<E> getAll()
+   {
+      ensureCorrectState();
+
+      //
+      return lazyList;
+   }
+
+   protected abstract ListAccess<E> create(S state);
+   
+   // Serialization
+
+   private void writeObject(ObjectOutputStream out) throws IOException
+   {
+      int pageSize;
+      try
+      {
+         pageSize = pageSizeField.getInt(this);
+      }
+      catch (IllegalAccessException e)
+      {
+         InvalidObjectException ioe = new InvalidObjectException("Cannot set page size");
+         ioe.initCause(e);
+         throw ioe;
+      }
+
+      //
+      out.writeInt(pageSize);
+      out.writeInt(currentPage_);
+      out.writeObject(state);
+   }
+
+   private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
+   {
+      try
+      {
+         pageSizeField.setInt(this, in.readInt());
+      }
+      catch (IllegalAccessException e)
+      {
+         InvalidObjectException ioe = new InvalidObjectException("Cannot set page size");
+         ioe.initCause(e);
+         throw ioe;
+      }
+      currentPage_ = in.readInt();
+      state = (S)in.readObject();
+   }
+   
+   // Intercept all method calls
+
+   @Override
+   public final int getAvailablePage()
+   {
+      ensureCorrectState();
+      return super.getAvailablePage();
+   }
+
+   @Override
+   public final int getTo()
+   {
+      ensureCorrectState();
+      return super.getTo();    
+   }
+
+   @Override
+   public final int getFrom()
+   {
+      ensureCorrectState();
+      return super.getFrom();
+   }
+
+   @Override
+   protected final void setAvailablePage(int available)
+   {
+      ensureCorrectState();
+      super.setAvailablePage(available);
+   }
+
+   @Override
+   protected final void checkAndSetPage(int page) throws Exception
+   {
+      ensureCorrectState();
+      super.checkAndSetPage(page);
+   }
+
+   @Override
+   public final List<E> getPage(int page) throws Exception
+   {
+      ensureCorrectState();
+      return super.getPage(page);
+   }
+
+   @Override
+   public final List<E> currentPage() throws Exception
+   {
+      ensureCorrectState();
+      return super.currentPage();
+   }
+
+   @Override
+   public final int getAvailable()
+   {
+      ensureCorrectState();
+      return super.getAvailable();
+   }
+
+   @Override
+   public final int getCurrentPage()
+   {
+      ensureCorrectState();
+      return super.getCurrentPage();
+   }
+
+   @Override
+   public final void setPageSize(int pageSize)
+   {
+      ensureCorrectState();
+      super.setPageSize(pageSize);
+   }
+
+   @Override
+   public final int getPageSize()
+   {
+      ensureCorrectState();
+      return super.getPageSize();
+   }
+}

Added: portal/trunk/component/common/src/test/java/org/exoplatform/commons/utils/StringPageListAccess.java
===================================================================
--- portal/trunk/component/common/src/test/java/org/exoplatform/commons/utils/StringPageListAccess.java	                        (rev 0)
+++ portal/trunk/component/common/src/test/java/org/exoplatform/commons/utils/StringPageListAccess.java	2010-01-18 00:14:27 UTC (rev 1340)
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.commons.utils;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class StringPageListAccess extends PageListAccess<String, String[]>
+{
+
+   public StringPageListAccess(int pageSize, String... state)
+   {
+      super(state, pageSize);
+   }
+
+   @Override
+   protected ListAccess<String> create(String[] state)
+   {
+      return new ArrayListAccess(state);
+   }
+
+   private static class ArrayListAccess implements ListAccess<String>
+   {
+
+      /** . */
+      private final String[] values;
+
+      private ArrayListAccess(String... values)
+      {
+         this.values = values;
+      }
+
+      public String[] load(int index, int length) throws Exception, IllegalArgumentException
+      {
+         String[] batch = new String[length];
+         System.arraycopy(values, index, batch, 0, length);
+         return batch;
+      }
+
+      public int getSize() throws Exception
+      {
+         return values.length;
+      }
+   }
+}

Added: portal/trunk/component/common/src/test/java/org/exoplatform/commons/utils/TestPageListAccess.java
===================================================================
--- portal/trunk/component/common/src/test/java/org/exoplatform/commons/utils/TestPageListAccess.java	                        (rev 0)
+++ portal/trunk/component/common/src/test/java/org/exoplatform/commons/utils/TestPageListAccess.java	2010-01-18 00:14:27 UTC (rev 1340)
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.commons.utils;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+
+import java.io.*;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class TestPageListAccess extends TestCase
+{
+
+   private final String[] l = {"0", "1", "2", "3", "4", "5", "6"};
+
+   public void testPageList() throws Exception
+   {
+      StringPageListAccess pageList = new StringPageListAccess(3, l);
+
+      //
+      assertState1(pageList);
+
+      //
+      pageList = clone(pageList);
+      assertState1(pageList);
+
+      //
+      List<String> list = pageList.getPage(1);
+      assertListState1(list);
+      assertState1(pageList);
+
+      //
+      pageList = clone(pageList);
+      list = pageList.getPage(1);
+      assertListState1(list);
+      assertState1(pageList);
+
+      //
+      list = pageList.getPage(2);
+      assertListState2(list);
+      assertState2(pageList);
+
+      //
+      pageList = clone(pageList);
+      list = pageList.getPage(2);
+      assertListState2(list);
+      assertState2(pageList);
+
+      //
+      list = pageList.getPage(3);
+      assertListState3(list);
+      assertState3(pageList);
+
+      //
+      pageList = clone(pageList);
+      list = pageList.getPage(3);
+      assertListState3(list);
+      assertState3(pageList);
+   }
+
+   private void assertListState3(List<String> list)
+   {
+      assertNotNull(list);
+      assertEquals(1, list.size());
+      assertEquals("6", list.get(0));
+   }
+
+   private void assertState3(PageList<String> list)
+   {
+      assertEquals(6, list.getFrom());
+      assertEquals(7, list.getTo());
+      assertEquals(3, list.getCurrentPage());
+      assertEquals(3, list.getAvailablePage());
+      assertEquals(7, list.getAvailable());
+   }
+
+   private void assertState2(PageList<String> list)
+   {
+      assertEquals(3, list.getFrom());
+      assertEquals(6, list.getTo());
+      assertEquals(2, list.getCurrentPage());
+      assertEquals(3, list.getAvailablePage());
+      assertEquals(7, list.getAvailable());
+   }
+
+   private void assertListState2(List<String> s)
+   {
+      assertNotNull(s);
+      assertEquals(3, s.size());
+      assertEquals("3", s.get(0));
+      assertEquals("4", s.get(1));
+      assertEquals("5", s.get(2));
+   }
+
+   private void assertListState1(List<String> s)
+   {
+      assertNotNull(s);
+      assertEquals(3, s.size());
+      assertEquals("0", s.get(0));
+      assertEquals("1", s.get(1));
+      assertEquals("2", s.get(2));
+   }
+
+   private void assertState1(PageList<String> list)
+   {
+      assertEquals(0, list.getFrom());
+      assertEquals(3, list.getTo());
+      assertEquals(1, list.getCurrentPage());
+      assertEquals(3, list.getAvailablePage());
+      assertEquals(7, list.getAvailable());
+   }
+
+
+   private StringPageListAccess clone(StringPageListAccess pageList)
+   {
+      try
+      {
+         ByteArrayOutputStream baos = new ByteArrayOutputStream();
+         ObjectOutputStream oos = new ObjectOutputStream(baos);
+         oos.writeObject(pageList);
+         oos.close();
+         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+         ObjectInputStream ois = new ObjectInputStream(bais);
+         return (StringPageListAccess)ois.readObject();
+      }
+      catch (Exception e)
+      {
+         AssertionFailedError afe = new AssertionFailedError();
+         afe.initCause(e);
+         throw afe;
+      }
+   }
+}



More information about the gatein-commits mailing list