Android Open Source - DKO Dual Iterator






From Project

Back to project page DKO.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project DKO listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.kered.dko;
//from   w w w  . j a  v  a  2s .  c  om
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import javax.sql.DataSource;

class DualIterator {

  private Connection conn;
  private PreparedStatement ps;
  private ResultSet rs;
  private boolean done = false;
  private boolean finishedNatually = false;
  private Queue<Object[]> leftQueue = new LinkedList<Object[]>();
  private Queue<Object[]> rightQueue = new LinkedList<Object[]>();
  InternalIterator leftIterator = new InternalIterator(leftQueue);
  InternalIterator rightIterator = new InternalIterator(rightQueue);
  private List<Field<?>> qLfields;
  private List<Field<?>> qRfields;
  private PeekableClosableIterator<Object[]> iterator = null;

  public DualIterator(DataSource ds, String sql, List<Field<?>> qLfields, List<Field<?>> qRfields) {
    this.qLfields = qLfields;
    this.qRfields = qRfields;
      Util.log(sql, null);
      try {
        conn = ds.getConnection();
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
      } catch (SQLException e) {
        throw new RuntimeException(e);
      }
  }

  public DualIterator(PeekableClosableIterator<Object[]> iterator, List<Field<?>> qLfields, List<Field<?>> qRfields) {
    this.qLfields = qLfields;
    this.qRfields = qRfields;
    conn = null;
    ps = null;
    rs = null;
    this.iterator = iterator;
  }

  public PeekableClosableIterator<Object[]> getLeftIterator() {
    return leftIterator;
  }

  public PeekableClosableIterator<Object[]> getRightIterator() {
    return rightIterator;
  }

  @Override
  protected void finalize() throws Throwable {
    if (!done) close();
    super.finalize();
  }

  public synchronized void close() {
    if (done) return;
    if (iterator!=null) iterator.close();
    try {
      if (!finishedNatually && rs!=null && ps!=null && !rs.isClosed()) {
        ps.cancel();
      }
    } catch (SQLException e2) {
      e2.printStackTrace();
    } catch (AbstractMethodError e) {
      if ("org.sqlite.RS".equals(rs.getClass().getName())) {
        // ignore - bad jdbc driver
      } else {
        throw e;
      }
    }
    try {
      if (rs!=null && !rs.isClosed()) rs.close();
    } catch (SQLException e2) {
      e2.printStackTrace();
    } catch (AbstractMethodError e) {
      if ("org.sqlite.RS".equals(rs.getClass().getName())) {
        // ignore - bad jdbc driver
      } else {
        throw e;
      }
    }
    try {
      if (ps!=null && !ps.isClosed()) ps.close();
    } catch (SQLException e1) {
      e1.printStackTrace();
    } catch (AbstractMethodError e) {
      if ("org.sqlite.RS".equals(rs.getClass().getName())) {
        // ignore - bad jdbc driver
      } else {
        throw e;
      }
    }
    try {
      if (conn!=null && !conn.isClosed()) conn.close();
    } catch (final SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    done = true;
  }
  
  public class InternalIterator implements PeekableClosableIterator<Object[]> {

    private Queue<Object[]> queue;
    private boolean closed = false;

    public InternalIterator(Queue<Object[]> queue) {
      this.queue = queue;
    }

    @Override
    public boolean hasNext() {
      if (queue.isEmpty()) getNextRow();
      return !queue.isEmpty();
    }

    @Override
    public Object[] next() {
      return queue.poll();
    }

    @Override
    public void remove() {
      throw new UnsupportedOperationException();
    }

    @Override
    public void close() {
      this.closed = true;
      if (leftIterator.closed && rightIterator.closed) {
        DualIterator.this.close();
      }
    }

    @Override
    public Object[] peek() {
      return queue.peek();
    }
    
  }

  public void getNextRow() {
    if (rs!=null) {
      try {
        if (!rs.next()) return;
        int leftSize = qLfields.size();
        final Object[] leftRow = new Object[leftSize];
        for (int i=0; i<leftSize; ++i) {
          leftRow[i] = Util.getTypedValueFromRS(rs, i+1, qLfields.get(i));
        }
        leftQueue.add(leftRow);
        int rightSize = qRfields.size();
        final Object[] rightRow = new Object[rightSize];
        for (int i=0; i<rightSize; ++i) {
          rightRow[i] = Util.getTypedValueFromRS(rs, leftSize+i+1, qRfields.get(i));
        }
        rightQueue.add(rightRow);
      } catch (SQLException e) {
        throw new RuntimeException(e);
      }
    } else if (iterator!=null) {
      if (!iterator.hasNext()) return;
      Object[] row = iterator.next();
      int leftSize = qLfields.size();
      final Object[] leftRow = new Object[leftSize];
      System.arraycopy(row, 0, leftRow, 0, leftSize);
      leftQueue.add(leftRow);
      int rightSize = qRfields.size();
      final Object[] rightRow = new Object[rightSize];
      System.arraycopy(row, leftSize, rightRow, 0, rightSize);
      rightQueue.add(rightRow);
    }
  }

}




Java Source Code List

.HelloWorld.java
org.kered.contactlensfinder.DB.java
org.kered.contactlensfinder.MainActivity.java
org.kered.contactlensfinder.ViewManufacturersActivity.java
org.kered.contactlensfinder.ViewProductsActivity.java
org.kered.contactlensfinder.ViewPropertiesActivity.java
org.kered.dko.AbstractQuery.java
org.kered.dko.Bulk.java
org.kered.dko.CSV.java
org.kered.dko.ClosableIterator.java
org.kered.dko.Condition.java
org.kered.dko.Constants.java
org.kered.dko.Context.java
org.kered.dko.DBQuery.java
org.kered.dko.DBRowIterator.java
org.kered.dko.Diff.java
org.kered.dko.DualIterator.java
org.kered.dko.Expression.java
org.kered.dko.Field.java
org.kered.dko.FilteringQuery.java
org.kered.dko.Function.java
org.kered.dko.InMemoryQuery.java
org.kered.dko.Join.java
org.kered.dko.LazyCacheIterable.java
org.kered.dko.LocalJoin.java
org.kered.dko.M.java
org.kered.dko.Main.java
org.kered.dko.MatryoshkaQuery.java
org.kered.dko.PeekableClosableIterator.java
org.kered.dko.PeekableIterator.java
org.kered.dko.QueryAddField.java
org.kered.dko.QueryFactory.java
org.kered.dko.QuerySnapshot.java
org.kered.dko.Query.java
org.kered.dko.SQLFunction.java
org.kered.dko.SelectAsMapIterable.java
org.kered.dko.SelectFromOAI.java
org.kered.dko.SelectSingleColumn.java
org.kered.dko.SoftJoinUtil.java
org.kered.dko.SoftJoin.java
org.kered.dko.SqlContext.java
org.kered.dko.Statistics.java
org.kered.dko.SubQueryField.java
org.kered.dko.TableInfo.java
org.kered.dko.TableWrapper.java
org.kered.dko.Table.java
org.kered.dko.TemporaryTableFactory.java
org.kered.dko.TmpTableBuilder.java
org.kered.dko.Tuple.java
org.kered.dko.UsageMonitor.java
org.kered.dko.UsageStats.java
org.kered.dko.Util.java
org.kered.dko.ant.ClassGenerator.java
org.kered.dko.ant.CodeGeneratorBase.java
org.kered.dko.ant.CodeGenerator.java
org.kered.dko.ant.DataSourceGenerator.java
org.kered.dko.ant.GsonGenerator.java
org.kered.dko.ant.JoinGenerator.java
org.kered.dko.ant.Main.java
org.kered.dko.ant.SchemaExtractorBase.java
org.kered.dko.ant.SchemaExtractor.java
org.kered.dko.ant.Util.java
org.kered.dko.datasource.CheapConnectionPoolingDataSource.java
org.kered.dko.datasource.ConnectionCountingDataSource.java
org.kered.dko.datasource.JDBCDriverDataSource.java
org.kered.dko.datasource.MatryoshkaDataSource.java
org.kered.dko.datasource.MirroredDataSource.java
org.kered.dko.datasource.ReflectedDataSource.java
org.kered.dko.datasource.SingleConnectionDataSource.java
org.kered.dko.datasource.SingleThreadedDataSource.java
org.kered.dko.datasource.UnClosableConnection.java
org.kered.dko.datasource.Util.java
org.kered.dko.json.CDL.java
org.kered.dko.json.CookieList.java
org.kered.dko.json.Cookie.java
org.kered.dko.json.HTTPTokener.java
org.kered.dko.json.HTTP.java
org.kered.dko.json.JSONArray.java
org.kered.dko.json.JSONException.java
org.kered.dko.json.JSONML.java
org.kered.dko.json.JSONObject.java
org.kered.dko.json.JSONString.java
org.kered.dko.json.JSONStringer.java
org.kered.dko.json.JSONTokener.java
org.kered.dko.json.JSONWriter.java
org.kered.dko.json.Pickle.java
org.kered.dko.json.XMLTokener.java
org.kered.dko.json.XML.java
org.kered.dko.junk.DerbyLoadTestSchema.java
org.kered.dko.junk.OracleCreateTestUser.java
org.kered.dko.junk.OracleLoadTestSchema.java
org.kered.dko.persistence.Util.java
org.kered.dko.util.DumpDatabase.java
sakila.Example0.java
sakila.Example1.java
sakila.Example2.java
sakila.Util.java