1 /* 2 * Copyright (c) 2012-2022, jcabi.com 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 1) Redistributions of source code must retain the above 8 * copyright notice, this list of conditions and the following 9 * disclaimer. 2) Redistributions in binary form must reproduce the above 10 * copyright notice, this list of conditions and the following 11 * disclaimer in the documentation and/or other materials provided 12 * with the distribution. 3) Neither the name of the jcabi.com nor 13 * the names of its contributors may be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 28 * OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 package com.jcabi.simpledb; 31 32 import com.amazonaws.services.simpledb.model.SelectRequest; 33 import com.amazonaws.services.simpledb.model.SelectResult; 34 import com.jcabi.aspects.Loggable; 35 import java.util.Iterator; 36 import java.util.NoSuchElementException; 37 import lombok.EqualsAndHashCode; 38 39 /** 40 * Iterator of items in SimpleDB. 41 * 42 * @since 0.1 43 */ 44 @Loggable(Loggable.DEBUG) 45 @EqualsAndHashCode(of = { "credentials", "table" }) 46 final class AwsIterator implements Iterator<Item> { 47 48 /** 49 * AWS credentials. 50 */ 51 private final transient Credentials credentials; 52 53 /** 54 * Domain name. 55 */ 56 private final transient String table; 57 58 /** 59 * Select request. 60 */ 61 private final transient SelectRequest request; 62 63 /** 64 * Most recent result. 65 */ 66 private transient SelectResult result; 67 68 /** 69 * Public ctor. 70 * @param creds Credentials 71 * @param name Domain name 72 * @param req Request 73 */ 74 AwsIterator(final Credentials creds, final String name, 75 final SelectRequest req) { 76 this.credentials = creds; 77 this.table = name; 78 this.request = req; 79 } 80 81 @Override 82 public boolean hasNext() { 83 if (this.result == null) { 84 this.result = this.credentials.aws().select(this.request); 85 } else if (this.result.getItems().isEmpty() 86 && this.result.getNextToken() != null) { 87 this.result = this.credentials.aws().select( 88 this.request.withNextToken(this.result.getNextToken()) 89 ); 90 } 91 return !this.result.getItems().isEmpty(); 92 } 93 94 @Override 95 public Item next() { 96 if (!this.hasNext()) { 97 throw new NoSuchElementException(); 98 } 99 return new AwsItem( 100 this.credentials, 101 this.table, 102 this.result.getItems().remove(0) 103 ); 104 } 105 106 @Override 107 public void remove() { 108 throw new UnsupportedOperationException(); 109 } 110 111 }