nom.tam.util.HashedList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nom-tam-fits Show documentation
Show all versions of nom-tam-fits Show documentation
Java library for reading and writing FITS files. FITS, the Flexible Image Transport System, is the format commonly used in the archiving and transport of astronomical data.
package nom.tam.util;
/*
* #%L
* nom.tam FITS library
* %%
* Copyright (C) 2004 - 2015 nom-tam-fits
* %%
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
* #L%
*/
/**
* This class implements a structure which can
* be accessed either through a hash or
* as linear list. Only some elements may have
* a hash key.
*
* This class is motivated by the FITS header
* structure where a user may wish to go through
* the header element by element, or jump directly
* to a given keyword. It assumes that all
* keys are unique. However, all elements in the
* structure need not have a key.
*
* This class does only the search structure
* and knows nothing of the semantics of the
* referenced objects.
*
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
/**
* a ordered hash map implementation.
*
* @param
* value of the map
*/
public class HashedList> implements Collection {
private static final class EntryComparator> implements Comparator {
private final Comparator comp;
private EntryComparator(Comparator comp) {
this.comp = comp;
}
@Override
public int compare(VALUE o1, VALUE o2) {
return this.comp.compare(o1.getKey(), o2.getKey());
}
}
private class HashedListIterator implements Cursor {
/**
* This index points to the value that would be returned in the next
* 'next' call.
*/
private int current;
HashedListIterator(int start) {
this.current = start;
}
@Override
public void add(String key, VALUE ref) {
add(ref);
}
@Override
public void add(VALUE reference) {
HashedList.this.add(this.current++, reference);
}
@Override
public VALUE end() {
this.current = Math.max(0, HashedList.this.ordered.size() - 1);
return next();
}
@Override
public boolean hasNext() {
return this.current >= 0 && this.current < HashedList.this.ordered.size();
}
@Override
public boolean hasPrev() {
return this.current > 0;
}
@Override
public VALUE next() {
if (this.current < 0 || this.current >= HashedList.this.ordered.size()) {
throw new NoSuchElementException("Outside list");
} else {
VALUE entry = HashedList.this.ordered.get(this.current);
this.current++;
return entry;
}
}
@Override
public VALUE next(int count) {
for (int index = 1; index < count; index++) {
next();
}
return next();
}
@Override
public VALUE prev() {
if (this.current <= 0) {
throw new NoSuchElementException("Before beginning of list");
}
return HashedList.this.ordered.get(--this.current);
}
@Override
public void remove() {
if (this.current > 0 && this.current <= HashedList.this.ordered.size()) {
HashedList.this.remove(--this.current);
}
}
@Override
public void setKey(String key) {
VALUE entry = HashedList.this.keyed.get(key);
if (entry != null) {
this.current = indexOf(entry);
} else {
this.current = HashedList.this.ordered.size();
}
}
}
/** An ordered list of the keys */
private final ArrayList ordered = new ArrayList();
/** The key value pairs */
private final HashMap keyed = new HashMap();
/**
* Add an element to the list at a specified position. If that element was
* already in the list, it is first removed from the list then added again
* - if it was removed from a position before the position where it was to
* be added, that position is decremented by one.
*
* @param pos
* The position at which the specified element is to be added.
* If pos is bigger than the size of the list the element is
* put at the end of the list.
* @param reference
* The element to add to the list.
*/
private void add(int pos, VALUE reference) {
VALUE entry = reference;
String key = entry.getKey();
if (this.keyed.containsKey(key) && !unkeyedKey(key)) {
int oldPos = indexOf(entry);
this.keyed.remove(key);
this.ordered.remove(oldPos);
if (oldPos < pos) {
pos--;
}
}
this.keyed.put(key, entry);
if (pos >= this.ordered.size()) {
this.ordered.add(entry);
} else {
this.ordered.add(pos, entry);
}
}
private static boolean unkeyedKey(String key) {
return "COMMENT".equals(key) || "HISTORY".equals(key) || key.trim().isEmpty();
}
@Override
public boolean add(VALUE e) {
add(this.ordered.size(), e);
return true;
}
@Override
public boolean addAll(Collection extends VALUE> c) {
for (VALUE element : c) {
add(element);
}
return true;
}
@Override
public void clear() {
this.keyed.clear();
this.ordered.clear();
}
@Override
public boolean contains(Object o) {
for (VALUE entry : this.ordered) {
if (o.equals(entry)) {
return true;
}
}
return false;
}
@Override
public boolean containsAll(Collection> c) {
List> values = new ArrayList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy