001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.http.part; 018 019import static org.apache.juneau.commons.utils.AssertionUtils.*; 020import static org.apache.juneau.commons.utils.ThrowableUtils.*; 021 022import java.util.*; 023 024import org.apache.http.*; 025import org.apache.juneau.commons.utils.*; 026 027/** 028 * Basic implementation of a {@link PartIterator}. 029 * 030 * <h5 class='section'>Notes:</h5><ul> 031 * <li class='warn'>This class is not thread safe. 032 * </ul> 033 * 034 * <h5 class='section'>See Also:</h5><ul> 035 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommonBasics">juneau-rest-common Basics</a> 036 * </ul> 037 */ 038public class BasicPartIterator implements PartIterator { 039 040 private final NameValuePair[] entries; 041 private final String name; 042 private final boolean caseInsensitive; 043 044 private int currentIndex; 045 046 /** 047 * Creates a new part iterator. 048 * 049 * @param parts An array of parts over which to iterate. 050 * @param name The name of the parts over which to iterate, or <jk>null</jk> for all. 051 * @param caseInsensitive Use case-insensitive matching for part name. 052 */ 053 public BasicPartIterator(NameValuePair[] parts, String name, boolean caseInsensitive) { 054 this.entries = assertArgNotNull("parts", parts); 055 this.name = name; 056 this.caseInsensitive = caseInsensitive; 057 this.currentIndex = findNext(-1); 058 } 059 060 @Override /* Overridden from Iterator */ 061 public boolean hasNext() { 062 return (currentIndex >= 0); 063 } 064 065 @Override /* Overridden from Iterator */ 066 public NameValuePair next() throws NoSuchElementException { 067 068 int current = currentIndex; 069 070 if (current < 0) 071 throw new NoSuchElementException("Iteration already finished."); 072 073 currentIndex = findNext(current); 074 075 return entries[current]; 076 } 077 078 /** 079 * Not supported. 080 */ 081 @Override /* Overridden from Iterator */ 082 public void remove() throws UnsupportedOperationException { 083 throw unsupportedOp(); 084 } 085 086 private boolean eq(String s1, String s2) { 087 return Utils.eq(caseInsensitive, s1, s2); // NOAI 088 } 089 090 private boolean filter(int index) { 091 return (name == null) || eq(name, entries[index].getName()); 092 } 093 094 private int findNext(int pos) { 095 096 int from = pos; 097 098 int to = entries.length - 1; 099 boolean found = false; 100 while (! found && (from < to)) { 101 from++; 102 found = filter(from); 103 } 104 105 return found ? from : -1; 106 } 107}