List of usage examples for org.apache.commons.collections.map HashedMap mapIterator
public MapIterator mapIterator()
From source file:com.octo.captcha.engine.bufferedengine.BufferedEngineContainer.java
/** * Method launch by a scheduler to swap captcha from disk buffer to the memory buffer. The ratio of swaping for each * locale is defined in the configuration component. *//* www. ja v a 2s . c o m*/ public void swapCaptchasFromPersistentToVolatileMemory() { log.debug("entering swapCaptchasFromDiskBufferToMemoryBuffer()"); MapIterator it = config.getLocaleRatio().mapIterator(); //construct the map of swap size by locales; HashedMap captchasRatios = new HashedMap(); while (it.hasNext()) { Locale locale = (Locale) it.next(); double ratio = ((Double) it.getValue()).doubleValue(); int ratioCount = (int) Math.round(config.getSwapSize().intValue() * ratio); //get the reminding size corresponding to the ratio int diff = (int) Math .round((config.getMaxVolatileMemorySize().intValue() - this.volatileBuffer.size()) * ratio); diff = diff < 0 ? 0 : diff; int toSwap = (diff < ratioCount) ? diff : ratioCount; captchasRatios.put(locale, new Integer(toSwap)); } //get them from persistent buffer Iterator captchasRatiosit = captchasRatios.mapIterator(); while (captchasRatiosit.hasNext() && !shutdownCalled) { Locale locale = (Locale) captchasRatiosit.next(); int swap = ((Integer) captchasRatios.get(locale)).intValue(); if (log.isDebugEnabled()) { log.debug("try to swap " + swap + " Captchas from persistent to volatile memory with locale : " + locale.toString()); } Collection temp = this.persistentBuffer.removeCaptcha(swap, locale); this.volatileBuffer.putAllCaptcha(temp, locale); if (log.isDebugEnabled()) { log.debug("swaped " + temp.size() + " Captchas from persistent to volatile memory with locale : " + locale.toString()); } //stats persistentMemoryHits += temp.size(); } if (log.isDebugEnabled()) { log.debug("new volatil Buffer size : " + volatileBuffer.size()); } // stats persistentToVolatileSwaps++; }