Java Code Examples for java.util.ArrayList.ensureCapacity()

The following are Jave code examples for showing how to use ensureCapacity() of the java.util.ArrayList class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Hello-Music-droid   File: MusicPlaybackState.java   Source Code and License Vote up 6 votes
public ArrayList<MusicPlaybackTrack> getQueue() {
    ArrayList<MusicPlaybackTrack> results = new ArrayList<>();

    Cursor cursor = null;
    try {
        cursor = mMusicDatabase.getReadableDatabase().query(PlaybackQueueColumns.NAME, null,
                null, null, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            results.ensureCapacity(cursor.getCount());

            do {
                results.add(new MusicPlaybackTrack(cursor.getLong(0), cursor.getLong(1),
                        TimberUtils.IdType.getTypeById(cursor.getInt(2)), cursor.getInt(3)));
            } while (cursor.moveToNext());
        }

        return results;
    } finally {
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
    }
}
 
Example 2
Project: KomaMusic   File: MusicPlaybackState.java   Source Code and License Vote up 6 votes
public ArrayList<MusicPlaybackTrack> getQueue() {
    ArrayList<MusicPlaybackTrack> results = new ArrayList<>();

    Cursor cursor = null;
    try {
        cursor = mMusicDatabase.getReadableDatabase().query(PlaybackQueueColumns.NAME, null,
                null, null, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            results.ensureCapacity(cursor.getCount());

            do {
                results.add(new MusicPlaybackTrack(cursor.getLong(0), cursor.getLong(1),
                        cursor.getInt(3)));
            } while (cursor.moveToNext());
        }

        return results;
    } finally {
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
    }
}
 
Example 3
Project: tomcat7   File: Parameters.java   Source Code and License Vote up 6 votes
@Deprecated
public void addParameterValues(String key, String[] newValues) {
    if (key == null) {
        return;
    }
    ArrayList<String> values = paramHashValues.get(key);
    if (values == null) {
        values = new ArrayList<String>(newValues.length);
        paramHashValues.put(key, values);
    } else {
        values.ensureCapacity(values.size() + newValues.length);
    }
    for (String newValue : newValues) {
        values.add(newValue);
    }
}
 
Example 4
Project: aos-Video   File: ManualShowScrappingSearchFragment.java   Source Code and License Vote up 6 votes
/**
 * Get the list of episodes in the DB for the given show
 * @param showId
 * @return
 */
private List<EpisodeTags> getEpisodeList(long showId) {
    ArrayList<EpisodeTags> result = new ArrayList<EpisodeTags>();
    // get EpisodeTags by ShowId
    final Uri uri = VideoStore.Video.Media.EXTERNAL_CONTENT_URI;
    final String selection = VideoStore.Video.VideoColumns.SCRAPER_SHOW_ID + "=?";
    final String[] selectionArgs = new String[] { String.valueOf(showId) };
    final String sortOrder = VideoStore.Video.VideoColumns.SCRAPER_E_SEASON+", "+VideoStore.Video.VideoColumns.SCRAPER_E_EPISODE;
    Cursor c = mContext.getContentResolver().query(uri, TagsFactory.VIDEO_COLUMNS, selection, selectionArgs, sortOrder);
    List<BaseTags> tagsList = TagsFactory.buildTagsFromVideoCursor(c);
    if (c != null)
        c.close();
    // add every EpisodeTags (should be all) to result list
    if (tagsList != null) {
        result.ensureCapacity(tagsList.size());
        for (BaseTags bTag : tagsList) {
            if (bTag instanceof EpisodeTags) {
                EpisodeTags epTag = (EpisodeTags) bTag;
                result.add(epTag);
            }
        }
    }
    return result;
}
 
Example 5
Project: alfresco-data-model   File: TypeConverter.java   Source Code and License Vote up 6 votes
/**
 * General conversion method to convert collection contents to the specified
 * type. Wrapper around the Collection version for arrays.
 * 
 * @param propertyType - the target property type
 * @param values - the value to be converted
 * @return - the converted value as the correct type
 * @throws DictionaryException if the property type's registered java class is invalid
 * @throws TypeConversionException if the conversion cannot be performed
 */
public final Collection<?> convert(DataTypeDefinition propertyType, Object[] values)
{
   if(values == null) {
      return convert(propertyType, (Collection<?>)null);
   } else {
      // Turn the array into a Collection, then convert as that
      ArrayList<Object> c = new ArrayList<Object>();
      c.ensureCapacity(values.length);
      for(Object v : values) {
         c.add(v);
      }
      // Convert
      return convert(propertyType, c);
   }
}
 
Example 6
Project: ctsms   File: ProbandListEntryTagValueBean.java   Source Code and License Vote up 6 votes
public static void copyProbandListEntryTagValuesInToJson(ArrayList<ProbandListEntryTagValueJsonVO> tagValuesOut, ArrayList<ProbandListEntryTagValueInVO> tagValuesIn,
		HashMap<Long, ProbandListEntryTagOutVO> tagVOsMap) {
	if (tagValuesOut != null) {
		tagValuesOut.clear();
		if (tagValuesIn != null && tagVOsMap != null) {
			tagValuesOut.ensureCapacity(tagValuesIn.size());
			Iterator<ProbandListEntryTagValueInVO> it = tagValuesIn.iterator();
			while (it.hasNext()) {
				ProbandListEntryTagValueInVO in = it.next();
				if (tagVOsMap.containsKey(in.getTagId())) {
					ProbandListEntryTagValueJsonVO out = new ProbandListEntryTagValueJsonVO();
					CommonUtil.copyProbandListEntryTagValueInToJson(out, in, tagVOsMap.get(in.getTagId()));
					tagValuesOut.add(out);
				}
			}
		}
	}
}
 
Example 7
Project: lazycat   File: Parameters.java   Source Code and License Vote up 6 votes
@Deprecated
public void addParameterValues(String key, String[] newValues) {
	if (key == null) {
		return;
	}
	ArrayList<String> values = paramHashValues.get(key);
	if (values == null) {
		values = new ArrayList<String>(newValues.length);
		paramHashValues.put(key, values);
	} else {
		values.ensureCapacity(values.size() + newValues.length);
	}
	for (String newValue : newValues) {
		values.add(newValue);
	}
}
 
Example 8
Project: ctsms   File: EcrfFieldValueBean.java   Source Code and License Vote up 6 votes
public static void copyEcrfFieldValuesInToJson(ArrayList<ECRFFieldValueJsonVO> ecrfFieldValuesOut, ArrayList<ECRFFieldValueInVO> ecrfFieldValuesIn,
		HashMap<Long, ECRFFieldOutVO> ecrfFieldVOsMap) {
	if (ecrfFieldValuesOut != null) {
		ecrfFieldValuesOut.clear();
		if (ecrfFieldValuesIn != null && ecrfFieldVOsMap != null) {
			ecrfFieldValuesOut.ensureCapacity(ecrfFieldValuesIn.size());
			Iterator<ECRFFieldValueInVO> it = ecrfFieldValuesIn.iterator();
			while (it.hasNext()) {
				ECRFFieldValueInVO in = it.next();
				if (ecrfFieldVOsMap.containsKey(in.getEcrfFieldId())) {
					ECRFFieldValueJsonVO out = new ECRFFieldValueJsonVO();
					CommonUtil.copyEcrfFieldValueInToJson(out, in, ecrfFieldVOsMap.get(in.getEcrfFieldId()));
					ecrfFieldValuesOut.add(out);
				}
			}
		}
	}
}
 
Example 9
Project: multiple-dimension-spread   File: MDSListObjectInspector.java   Source Code and License Vote up 5 votes
@Override
public Object resize( final Object object ,  final int newSize ){
  ArrayList list = (ArrayList) object;
  list.ensureCapacity(newSize);
  while (list.size() < newSize) {
    list.add(null);
  }
  while (list.size() > newSize) {
    list.remove(list.size() - 1);
  }
  return list;
}
 
Example 10
Project: myfaces-trinidad   File: BaseConverter.java   Source Code and License Vote up 5 votes
@SuppressWarnings("unchecked")
@Override
public List<Class<?>> getTargetTypes(Class<?> sourceType)
{
  ArrayList<Class<?>> list = new ArrayList<Class<?>>(1);
  if (Date.class.isAssignableFrom(sourceType))
  {
    list.add(Calendar.class);
  }
  else if (Calendar.class.isAssignableFrom(sourceType))
  {
    list.add(Date.class);
  }
  else if (Number.class.isAssignableFrom(sourceType))
  {
    list.ensureCapacity(14);
    list.add(Byte.class);
    list.add(Double.class);
    list.add(Float.class);
    list.add(Integer.class);
    list.add(Long.class);
    list.add(Short.class);
    list.add(BigDecimal.class);      
    list.add(Byte.TYPE);
    list.add(Double.TYPE);
    list.add(Float.TYPE); // bug 4891181
    list.add(Integer.TYPE);
    list.add(Long.TYPE);
    list.add(Short.TYPE);
    list.add (BigInteger.class);
  }
  
  return list;
}
 
Example 11
Project: myfaces-trinidad   File: FacesBean.java   Source Code and License Vote up 5 votes
static private void _expandListToIndex(ArrayList<PropertyKey> list, int count)
{
  list.ensureCapacity(count + 1);
  int addCount = (count + 1) - list.size();
  for (int i = 0; i < addCount; i++)
    list.add(null);
}
 
Example 12
Project: myfaces-trinidad   File: UIXSelectManyTemplate.java   Source Code and License Vote up 5 votes
@SuppressWarnings("unchecked")
static List<Object> __toList(Object o)
{
  if (o == null)
    return null;

  ArrayList<Object> listCopy = new ArrayList<Object>();
  if (o instanceof Collection)
  {
    listCopy.addAll((Collection<Object>) o);
  }
  // Use java.lang.reflect.Array, etc. to support primitive Object arrays
  else if (o.getClass().isArray())
  {
    int length = Array.getLength(o);
    listCopy.ensureCapacity(length);
    for (int i = 0; i < length; i++)
    {
      listCopy.add(Array.get(o, i));
    }
  }
  else
  {
    // Already logged in __getSize(), so let it slide here
    ;
  }

  return listCopy;
}
 
Example 13
Project: letv   File: DataEntity.java   Source Code and License Vote up 5 votes
public static ArrayList<DataEntity> readFromParcel(Parcel src) {
    ArrayList<DataEntity> result = new ArrayList();
    try {
        int size = src.readInt();
        result.ensureCapacity(size);
        for (int i = 0; i < size; i++) {
            result.add(i, new DataEntity(src));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 14
Project: aos-Video   File: VideoInfoShowScraperFragment.java   Source Code and License Vote up 5 votes
private List<EpisodeTags> getEpisodeList(ShowTags sTag) {
    ArrayList<EpisodeTags> result = new ArrayList<EpisodeTags>();
    if (sTag != null) {
        // get EpisodeTags by ShowId
        long sId = sTag.getId();
        ContentResolver cr = mContext.getContentResolver();
        Uri uri = VideoStore.Video.Media.EXTERNAL_CONTENT_URI;
        String selection = VideoStore.Video.VideoColumns.SCRAPER_SHOW_ID + "=?";
        String[] selectionArgs = new String[] {
                String.valueOf(sId)
        };
        String sortOrder = null;
        Cursor c = cr.query(uri, TagsFactory.VIDEO_COLUMNS, selection, selectionArgs, sortOrder);
        List<BaseTags> tagsList = TagsFactory.buildTagsFromVideoCursor(c);
        if (c != null)
            c.close();
        // add every EpisodeTags (should be all) to result list
        if (tagsList != null) {
            result.ensureCapacity(tagsList.size());
            for (BaseTags bTag : tagsList) {
                if (bTag instanceof EpisodeTags) {
                    EpisodeTags epTag = (EpisodeTags) bTag;
                    result.add(epTag);
                }
            }
        }
    }
    return result;
}
 
Example 15
Project: jdk8u-jdk   File: EnsureCapacity.java   Source Code and License Vote up 5 votes
private static void testArrayList() {
    ArrayList<String> al = new ArrayList<String>();
    al.add("abc");
    al.ensureCapacity(Integer.MIN_VALUE);

    // there is no method to query the capacity of ArrayList
    // so before and after capacity are not checked
}
 
Example 16
Project: openjdk-jdk10   File: ArrayManagement.java   Source Code and License Vote up 5 votes
static void ensureCapacity(ArrayList<Object> list, int capacity) {
    int oldCapacity = capacity(list);
    int oldModCount = modCount(list);
    list.ensureCapacity(capacity);
    assertTrue(capacity(list) >= capacity || capacity(list) == 0);
    assertEquals(modCount(list),
                 (capacity(list) == oldCapacity)
                 ? oldModCount
                 : oldModCount + 1);
}
 
Example 17
Project: openjdk-jdk10   File: ArrayManagement.java   Source Code and License Vote up 5 votes
@Test public void ensureCapacityBeyondDefaultCapacity() {
    ArrayList<Object> list = new ArrayList<>();
    list.ensureCapacity(DEFAULT_CAPACITY + 1);
    assertEquals(capacity(list), DEFAULT_CAPACITY + 1);
    for (int i = 0; i < DEFAULT_CAPACITY + 1; i++) {
        addOneElement(list);
        assertEquals(capacity(list), DEFAULT_CAPACITY + 1);
    }
    addOneElement(list);
    assertEquals(capacity(list), newCapacity(DEFAULT_CAPACITY + 1));
}
 
Example 18
Project: openjdk-jdk10   File: EnsureCapacity.java   Source Code and License Vote up 5 votes
private static void testArrayList() {
    ArrayList<String> al = new ArrayList<String>();
    al.add("abc");
    al.ensureCapacity(Integer.MIN_VALUE);

    // there is no method to query the capacity of ArrayList
    // so before and after capacity are not checked
}
 
Example 19
Project: ctsms   File: SearchServiceImpl.java   Source Code and License Vote up 5 votes
private void prepareInventoryDistinctColumns(SearchResultExcelWriter writer, Collection inventoryVOs, ArrayList<String> distinctColumnNames,
		HashMap<Long, HashMap<String, Object>> distinctFieldRows) throws Exception {
	boolean showTags = Settings.getBoolean(SearchResultExcelSettingCodes.SHOW_INVENTORY_TAGS, Bundle.SEARCH_RESULT_EXCEL, SearchResultExcelDefaultSettings.SHOW_INVENTORY_TAGS);
	InventoryTagDao inventoryTagDao = this.getInventoryTagDao();
	Collection inventoryTags = showTags ? inventoryTagDao.findByVisibleIdExcel(null, null, true) : new ArrayList();
	inventoryTagDao.toInventoryTagVOCollection(inventoryTags);
	distinctColumnNames.ensureCapacity(inventoryTags.size());
	Iterator<InventoryTagVO> inventoryTagsIt = inventoryTags.iterator();
	while (inventoryTagsIt.hasNext()) {
		distinctColumnNames.add(inventoryTagsIt.next().getName());
	}
	InventoryTagValueDao inventoryTagValueDao = this.getInventoryTagValueDao();
	Iterator<InventoryOutVO> inventoryVOsIt = inventoryVOs.iterator();
	while (inventoryVOsIt.hasNext()) {
		InventoryOutVO inventoryVO = inventoryVOsIt.next();
		HashMap<String, Object> fieldRow = new HashMap<String, Object>(distinctColumnNames.size());
		Collection tagValues = showTags ? inventoryTagValueDao.findByInventory(inventoryVO.getId(), null) : new ArrayList<InventoryTagValue>();
		inventoryTagValueDao.toInventoryTagValueOutVOCollection(tagValues);
		Iterator<InventoryTagValueOutVO> tagValuesIt = tagValues.iterator();
		while (tagValuesIt.hasNext()) {
			InventoryTagValueOutVO tagValueOutVO = tagValuesIt.next();
			StringBuilder fieldValue;
			if (fieldRow.containsKey(tagValueOutVO.getTag().getName())) {
				fieldValue = new StringBuilder((String) fieldRow.get(tagValueOutVO.getTag().getName()));
			} else {
				fieldValue = new StringBuilder();
			}
			if (fieldValue.length() > 0) {
				fieldValue.append(ExcelUtil.EXCEL_LINE_BREAK);
			}
			fieldValue.append(tagValueOutVO.getValue());
			fieldRow.put(tagValueOutVO.getTag().getName(), fieldValue.toString());
		}
		distinctFieldRows.put(inventoryVO.getId(), fieldRow);
	}
}
 
Example 20
Project: JarShrink   File: Lists.java   Source Code and License Vote up 3 votes
public static <T> void addAll(List<T> list, T[] array) {
	
	if (list instanceof ArrayList) {
		
		ArrayList<T> a = (ArrayList<T>)list;
		a.ensureCapacity(a.size()+array.length);
	}
	
	for (T t : array) {
		
		list.add(t);
	}
}