Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package nom.tam.util;
/*
* #%L
* nom.tam FITS library
* %%
* Copyright (C) 2004 - 2024 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;
import nom.tam.fits.header.FitsKey;
/**
* An 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 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) {
current = start;
}
@Override
public void add(String key, VALUE ref) {
add(ref);
}
@Override
public void add(VALUE reference) {
HashedList.this.add(current, reference);
current++;
// AK: Do not allow the iterator to exceed the header size
// prev() requires this to work properly...
if (current > HashedList.this.size()) {
current = HashedList.this.size();
}
}
@Override
public VALUE end() {
current = HashedList.this.ordered.size() - 1;
if (current < 0) {
current = 0;
return null;
}
return next();
}
@Override
public boolean hasNext() {
return current >= 0 && current < HashedList.this.ordered.size();
}
@Override
public boolean hasPrev() {
return current > 0;
}
@Override
public VALUE next() {
if (current < 0 || current >= HashedList.this.ordered.size()) {
throw new NoSuchElementException("Outside list: " + current);
}
VALUE entry = HashedList.this.ordered.get(current);
current++;
return entry;
}
@Override
public VALUE next(int count) {
for (int index = 1; index < count; index++) {
next();
}
return next();
}
@Override
public VALUE prev() {
if (current <= 0) {
throw new NoSuchElementException("Before beginning of list");
}
return HashedList.this.ordered.get(--current);
}
@Override
public void remove() {
if (current > 0 && current <= HashedList.this.ordered.size()) {
HashedList.this.remove(--current);
}
}
@Override
public void setKey(String key) {
VALUE entry = HashedList.this.keyed.get(key);
if (entry != null) {
current = indexOf(entry);
} else {
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<>();
/**
* This maintains a 'current' position in the list...
*/
private HashedListIterator cursor = new HashedListIterator(0);
/**
* 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 (and 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 entry) {
String key = entry.getKey();
if (keyed.containsKey(key) && !FitsKey.isCommentStyleKey(key)) {
int oldPos = indexOf(entry);
internalRemove(oldPos, entry);
if (oldPos < pos) {
pos--;
}
}
keyed.put(key, entry);
if (pos >= ordered.size()) {
// AK: We are adding a card to the end of the header.
// If the cursor points to the end of the header, we want to increment it.
// We can do this by faking 'insertion' before the last position.
// The cursor will then advance at the end of this method.
// Note, that if the addition of the card was done through the cursor itself
// then the cursor will be incremented twice, once here, and once by the
// cursor itself by the HashedListIterator.add(call).
// But, this is fine, since the end position is properly checked by
// HashedListIterator.add().
pos = ordered.size() - 1;
ordered.add(entry);
} else {
ordered.add(pos, entry);
}
// AK: When inserting keys before the current position, increment the current
// position so it keeps pointing to the same location in the header...
if (pos < cursor.current) {
cursor.current++;
}
}
@Override
public boolean add(VALUE e) {
add(ordered.size(), e);
return true;
}
/**
* Similar to add(VALUE), except this replaces an existing card that matches the specified key in-situ. At the same
* time, new entries are added at the current position.
*
* @param key The key of the existing card (if any) to be replaced).
* @param entry The element to add to the list.
*/
public void update(String key, VALUE entry) {
if (keyed.containsKey(key) && !FitsKey.isCommentStyleKey(key)) {
int index = indexOf(get(key));
remove(index);
add(index, entry);
} else {
cursor.add(entry);
}
}
@Override
public boolean addAll(Collection extends VALUE> c) {
for (VALUE element : c) {
add(element);
}
return true;
}
@Override
public void clear() {
keyed.clear();
ordered.clear();
}
@Override
public boolean contains(Object o) {
for (VALUE entry : ordered) {
if (o.equals(entry)) {
return true;
}
}
return false;
}
@Override
public boolean containsAll(Collection> c) {
List> values = new ArrayList