📄 cosarraylist.java
字号:
*
* @return The list of String objects.
*/
public static List convertCOSStringCOSArrayToList( COSArray stringArray )
{
List retval = null;
if( stringArray != null )
{
List string = new ArrayList();
for( int i=0; i<stringArray.size(); i++ )
{
string.add( ((COSString)stringArray.getObject( i )).getString() );
}
retval = new COSArrayList( string, stringArray );
}
return retval;
}
/**
* This will take an list of string objects and return a COSArray of COSName
* objects.
*
* @param strings A list of strings
*
* @return An array of COSName objects
*/
public static COSArray convertStringListToCOSNameCOSArray( List strings )
{
COSArray retval = new COSArray();
for( int i=0; i<strings.size(); i++ )
{
Object next = strings.get( i );
if( next instanceof COSName )
{
retval.add( (COSName)next );
}
else
{
retval.add( COSName.getPDFName( (String)next ) );
}
}
return retval;
}
/**
* This will take an list of string objects and return a COSArray of COSName
* objects.
*
* @param strings A list of strings
*
* @return An array of COSName objects
*/
public static COSArray convertStringListToCOSStringCOSArray( List strings )
{
COSArray retval = new COSArray();
for( int i=0; i<strings.size(); i++ )
{
retval.add( new COSString( (String)strings.get( i ) ) );
}
return retval;
}
/**
* This will convert a list of COSObjectables to an
* array list of COSBase objects.
*
* @param cosObjectableList A list of COSObjectable.
*
* @return A list of COSBase.
*/
public static COSArray converterToCOSArray( List cosObjectableList )
{
COSArray array = null;
if( cosObjectableList != null )
{
if( cosObjectableList instanceof COSArrayList )
{
//if it is already a COSArrayList then we don't want to recreate the array, we want to reuse it.
array = ((COSArrayList)cosObjectableList).array;
}
else
{
array = new COSArray();
Iterator iter = cosObjectableList.iterator();
while( iter.hasNext() )
{
Object next = iter.next();
if( next instanceof String )
{
array.add( new COSString( (String)next ) );
}
else if( next instanceof Integer || next instanceof Long )
{
array.add( new COSInteger( ((Number)next).longValue() ) );
}
else if( next instanceof Float || next instanceof Double )
{
array.add( new COSFloat( ((Number)next).floatValue() ) );
}
else if( next instanceof COSObjectable )
{
COSObjectable object = (COSObjectable)next;
array.add( object.getCOSObject() );
}
else if( next instanceof DualCOSObjectable )
{
DualCOSObjectable object = (DualCOSObjectable)next;
array.add( object.getFirstCOSObject() );
array.add( object.getSecondCOSObject() );
}
else if( next == null )
{
array.add( COSNull.NULL );
}
else
{
throw new RuntimeException( "Error: Don't know how to convert type to COSBase '" +
next.getClass().getName() + "'" );
}
}
}
}
return array;
}
private List toCOSObjectList( Collection list )
{
List cosObjects = new ArrayList();
Iterator iter = list.iterator();
while( iter.hasNext() )
{
Object next = iter.next();
if( next instanceof String )
{
cosObjects.add( new COSString( (String)next ) );
}
else if( next instanceof DualCOSObjectable )
{
DualCOSObjectable object = (DualCOSObjectable)next;
array.add( object.getFirstCOSObject() );
array.add( object.getSecondCOSObject() );
}
else
{
COSObjectable cos = (COSObjectable)next;
cosObjects.add( cos.getCOSObject() );
}
}
return cosObjects;
}
/**
* {@inheritDoc}
*/
public boolean removeAll(Collection c)
{
array.removeAll( toCOSObjectList( c ) );
return actual.removeAll( c );
}
/**
* {@inheritDoc}
*/
public boolean retainAll(Collection c)
{
array.retainAll( toCOSObjectList( c ) );
return actual.retainAll( c );
}
/**
* {@inheritDoc}
*/
public void clear()
{
//when adding if there is a parentDict then change the item
//in the dictionary from a single item to an array.
if( parentDict != null )
{
parentDict.setItem( dictKey, (COSBase)null );
}
actual.clear();
array.clear();
}
/**
* {@inheritDoc}
*/
public boolean equals(Object o)
{
return actual.equals( o );
}
/**
* {@inheritDoc}
*/
public int hashCode()
{
return actual.hashCode();
}
/**
* {@inheritDoc}
*/
public Object get(int index)
{
return actual.get( index );
}
/**
* {@inheritDoc}
*/
public Object set(int index, Object element)
{
if( element instanceof String )
{
COSString item = new COSString( (String)element );
if( parentDict != null && index == 0 )
{
parentDict.setItem( dictKey, item );
}
array.set( index, item );
}
else if( element instanceof DualCOSObjectable )
{
DualCOSObjectable dual = (DualCOSObjectable)element;
array.set( index*2, dual.getFirstCOSObject() );
array.set( index*2+1, dual.getSecondCOSObject() );
}
else
{
if( parentDict != null && index == 0 )
{
parentDict.setItem( dictKey, ((COSObjectable)element).getCOSObject() );
}
array.set( index, ((COSObjectable)element).getCOSObject() );
}
return actual.set( index, element );
}
/**
* {@inheritDoc}
*/
public void add(int index, Object element)
{
//when adding if there is a parentDict then change the item
//in the dictionary from a single item to an array.
if( parentDict != null )
{
parentDict.setItem( dictKey, array );
//clear the parent dict so it doesn't happen again, there might be
//a usecase for keeping the parentDict around but not now.
parentDict = null;
}
actual.add( index, element );
if( element instanceof String )
{
array.add( index, new COSString( (String)element ) );
}
else if( element instanceof DualCOSObjectable )
{
DualCOSObjectable dual = (DualCOSObjectable)element;
array.add( index*2, dual.getFirstCOSObject() );
array.add( index*2+1, dual.getSecondCOSObject() );
}
else
{
array.add( index, ((COSObjectable)element).getCOSObject() );
}
}
/**
* {@inheritDoc}
*/
public Object remove(int index)
{
if( array.size() > index && array.get( index ) instanceof DualCOSObjectable )
{
//remove both objects
array.remove( index );
array.remove( index );
}
else
{
array.remove( index );
}
return actual.remove( index );
}
/**
* {@inheritDoc}
*/
public int indexOf(Object o)
{
return actual.indexOf( o );
}
/**
* {@inheritDoc}
*/
public int lastIndexOf(Object o)
{
return actual.indexOf( o );
}
/**
* {@inheritDoc}
*/
public ListIterator listIterator()
{
return actual.listIterator();
}
/**
* {@inheritDoc}
*/
public ListIterator listIterator(int index)
{
return actual.listIterator( index );
}
/**
* {@inheritDoc}
*/
public List subList(int fromIndex, int toIndex)
{
return actual.subList( fromIndex, toIndex );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -