PageRenderTime 41ms CodeModel.GetById 18ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/google/appengine/datanucleus/query/RuntimeExceptionWrappingIterator.java

http://datanucleus-appengine.googlecode.com/
Java | 113 lines | 77 code | 13 blank | 23 comment | 5 complexity | 12808f7beb8988c13c137583a77ae0da MD5 | raw file
  1/**********************************************************************
  2 Copyright (c) 2009 Google Inc.
  3
  4 Licensed under the Apache License, Version 2.0 (the "License");
  5 you may not use this file except in compliance with the License.
  6 You may obtain a copy of the License at
  7
  8 http://www.apache.org/licenses/LICENSE-2.0
  9
 10 Unless required by applicable law or agreed to in writing, software
 11 distributed under the License is distributed on an "AS IS" BASIS,
 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 See the License for the specific language governing permissions and
 14 limitations under the License.
 15 **********************************************************************/
 16package com.google.appengine.datanucleus.query;
 17
 18import com.google.appengine.api.datastore.Entity;
 19import com.google.appengine.datanucleus.Utils.Supplier;
 20
 21import java.util.Iterator;
 22
 23import org.datanucleus.api.ApiAdapter;
 24
 25/**
 26 * {@link Iterator} implementation that catches runtime exceptions thrown by
 27 * the datastore api and translates them to the appropriate JDO or JPA
 28 * exception.  DataNucleus is supposed to do this for us, but they miss runtime
 29 * exceptions that are thrown while iterating.
 30 *
 31 * @author Max Ross <maxr@google.com>
 32 */
 33class RuntimeExceptionWrappingIterator implements Iterator<Entity> {
 34
 35  final ApiAdapter api;
 36
 37  final Iterator<Entity> inner;
 38  final Supplier<Boolean> hasNextSupplier;
 39  final Supplier<Entity> nextSupplier;
 40  final Supplier<Void> removeSupplier;
 41
 42  RuntimeExceptionWrappingIterator(final ApiAdapter api, final Iterator<Entity> inner,
 43                                   final RuntimeExceptionObserver exceptionObserver) {
 44    if (inner == null) {
 45      throw new NullPointerException("inner cannot be null");
 46    }
 47    this.api = api;
 48    this.inner = inner;
 49    Supplier<Boolean> datastoreHasNextSupplier = QueryExceptionWrappers.datastoreToDataNucleus(
 50        new Supplier<Boolean>() {
 51          public Boolean get() {
 52            boolean success = false;
 53            try {
 54              boolean result = inner.hasNext();
 55              success = true;
 56              return result;
 57            } finally {
 58              if (!success) {
 59                exceptionObserver.onException();
 60              }
 61            }
 62          }
 63        });
 64
 65    Supplier<Entity> datastoreNextSupplier = QueryExceptionWrappers.datastoreToDataNucleus(
 66        new Supplier<Entity>() {
 67          public Entity get() {
 68            boolean success = false;
 69            try {
 70              Entity result = inner.next();
 71              success = true;
 72              return result;
 73            } finally {
 74              if (!success) {
 75                exceptionObserver.onException();
 76              }
 77            }
 78          }
 79        });
 80
 81    Supplier<Void> datastoreRemoveSupplier = QueryExceptionWrappers.datastoreToDataNucleus(
 82          new Supplier<Void>() {
 83            public Void get() {
 84              boolean success = false;
 85              try {
 86                inner.remove();
 87                success = true;
 88                return null;
 89              } finally {
 90                if (!success) {
 91                  exceptionObserver.onException();
 92                }
 93              }
 94            }
 95          });
 96
 97    hasNextSupplier = QueryExceptionWrappers.dataNucleusToApi(api, datastoreHasNextSupplier);
 98    nextSupplier = QueryExceptionWrappers.dataNucleusToApi(api, datastoreNextSupplier);
 99    removeSupplier = QueryExceptionWrappers.dataNucleusToApi(api, datastoreRemoveSupplier);
100  }
101
102  public boolean hasNext() {
103    return hasNextSupplier.get();
104  }
105
106  public Entity next() {
107    return nextSupplier.get();
108  }
109
110  public void remove() {
111    removeSupplier.get();
112  }
113}