Example usage for java.util Collections synchronizedMap

List of usage examples for java.util Collections synchronizedMap

Introduction

In this page you can find the example usage for java.util Collections synchronizedMap.

Prototype

public static <K, V> Map<K, V> synchronizedMap(Map<K, V> m) 

Source Link

Document

Returns a synchronized (thread-safe) map backed by the specified map.

Usage

From source file:org.apache.hadoop.hbase.client.crosssite.CrossSiteHTable.java

@Override
public <R extends Message> Map<byte[], R> batchCoprocessorService(Descriptors.MethodDescriptor methodDescriptor,
        Message request, byte[] startKey, byte[] endKey, R responsePrototype)
        throws ServiceException, Throwable {
    final Map<byte[], R> results = Collections.synchronizedMap(new TreeMap<byte[], R>(Bytes.BYTES_COMPARATOR));
    batchCoprocessorService(methodDescriptor, request, startKey, endKey, responsePrototype, new Callback<R>() {

        @Override//from ww  w  .j a va 2s . c o m
        public void update(byte[] region, byte[] row, R result) {
            if (region != null) {
                results.put(region, result);
            }
        }
    });
    return results;
}

From source file:org.opencms.loader.CmsJspLoader.java

/**
 * Initializes the caches.<p>/*w w w  .j av  a 2  s  .  co  m*/
 * 
 * @param cacheSize the cache size 
 */
protected void initCaches(int cacheSize) {

    Map<String, Boolean> map = CmsCollectionsGenericWrapper.createLRUMap(cacheSize);
    m_offlineJsps = Collections.synchronizedMap(map);
    map = CmsCollectionsGenericWrapper.createLRUMap(cacheSize);
    m_onlineJsps = Collections.synchronizedMap(map);
}

From source file:co.cask.cdap.common.conf.Configuration.java

/**
 * Load a class by name, returning null rather than throwing an exception
 * if it couldn't be loaded. This is to avoid the overhead of creating
 * an exception./* w  w  w  .  ja va  2  s. co  m*/
 *
 * @param name the class name
 * @return the class object, or null if it could not be found.
 */
public Class<?> getClassByNameOrNull(String name) {
    Map<String, Class<?>> map;

    synchronized (CACHE_CLASSES) {
        map = CACHE_CLASSES.get(classLoader);
        if (map == null) {
            map = Collections.synchronizedMap(new WeakHashMap<String, Class<?>>());
            CACHE_CLASSES.put(classLoader, map);
        }
    }

    Class<?> clazz = map.get(name);
    if (clazz == null) {
        try {
            clazz = Class.forName(name, true, classLoader);
        } catch (ClassNotFoundException e) {
            // Leave a marker that the class isn't found
            map.put(name, NEGATIVE_CACHE_SENTINEL);
            return null;
        }
        // two putters can race here, but they'll put the same class
        map.put(name, clazz);
        return clazz;
    } else if (clazz == NEGATIVE_CACHE_SENTINEL) {
        return null; // not found
    } else {
        // cache hit
        return clazz;
    }
}

From source file:org.dihedron.webmvc.ActionContext.java

/**
 * Sets interceptor-specific data into the action context; this information
 * is available through different calls and can be used to keep track of
 * system status, such as number of calls for target, or number of accesses
 * by the same user etc. This method should only be used by interceptors,
 * and the associated data should not be tampered with, to avoid
 * unpredictable behaviour./*w  w w.j a v a  2  s .c o m*/
 * 
 * @param interceptorId
 *   the namepaced id of the interceptor this data belongs to (see
 *   {@link Interceptor#getId()} for details).
 * @param data
 *   the data object.
 * @throws WebMVCException
 */
public static void setInterceptorData(String interceptorId, Object data) throws WebMVCException {
    @SuppressWarnings("unchecked")
    Map<String, Object> map = (Map<String, Object>) getValue(INTERCEPTOR_DATA_KEY, Scope.SESSION);
    if (map == null) {
        map = Collections.synchronizedMap(new HashMap<String, Object>());
        setValue(INTERCEPTOR_DATA_KEY, map, Scope.SESSION);
    }
    map.put(interceptorId, data);
}

From source file:org.opencms.monitor.CmsMemoryMonitor.java

/**
 * Initializes the monitor with the provided configuration.<p>
 * //from   ww  w.  j  ava 2 s.c o  m
 * @param configuration the configuration to use
 */
public void initialize(CmsSystemConfiguration configuration) {

    CmsCacheSettings cacheSettings = configuration.getCacheSettings();

    m_memoryAverage = new CmsMemoryStatus();
    m_memoryCurrent = new CmsMemoryStatus();

    m_warningSendSinceLastStatus = false;
    m_warningLoggedSinceLastStatus = false;
    m_lastEmailWarning = 0;
    m_lastEmailStatus = 0;
    m_lastLogStatus = 0;
    m_lastLogWarning = 0;
    m_lastClearCache = 0;
    m_configuration = configuration.getCmsMemoryMonitorConfiguration();

    m_intervalWarning = 720 * 60000;
    m_maxUsagePercent = 90;

    m_intervalEmail = m_configuration.getEmailInterval() * 1000;
    m_intervalLog = m_configuration.getLogInterval() * 1000;

    if (m_configuration.getWarningInterval() > 0) {
        m_intervalWarning = m_configuration.getWarningInterval();
    }
    m_intervalWarning *= 1000;

    if (m_configuration.getMaxUsagePercent() > 0) {
        m_maxUsagePercent = m_configuration.getMaxUsagePercent();
    }

    if (CmsLog.INIT.isInfoEnabled()) {
        CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_MM_INTERVAL_LOG_1,
                new Integer(m_intervalLog / 1000)));
        CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_MM_INTERVAL_EMAIL_1,
                new Integer(m_intervalEmail / 1000)));
        CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_MM_INTERVAL_WARNING_1,
                new Integer(m_intervalWarning / 1000)));
        CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_MM_INTERVAL_MAX_USAGE_1,
                new Integer(m_maxUsagePercent)));

        if ((m_configuration.getEmailReceiver() == null) || (m_configuration.getEmailSender() == null)) {
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_MM_EMAIL_DISABLED_0));
        } else {
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_MM_EMAIL_SENDER_1,
                    m_configuration.getEmailSender()));
            Iterator<String> i = m_configuration.getEmailReceiver().iterator();
            int n = 0;
            while (i.hasNext()) {
                CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_MM_EMAIL_RECEIVER_2,
                        new Integer(n + 1), i.next()));
                n++;
            }
        }
    }

    // create and register all system caches

    // temporary xml entities cache
    Map<String, byte[]> xmlTemporaryCache = CmsCollectionsGenericWrapper.createLRUMap(128);
    m_cacheXmlTemporaryEntity = Collections.synchronizedMap(xmlTemporaryCache);
    register(CmsXmlEntityResolver.class.getName() + ".xmlEntityTemporaryCache", m_cacheXmlTemporaryEntity);

    // permanent xml entities cache
    Map<String, byte[]> xmlPermanentCache = new HashMap<String, byte[]>(32);
    m_cacheXmlPermanentEntity = Collections.synchronizedMap(xmlPermanentCache);
    register(CmsXmlEntityResolver.class.getName() + ".xmlEntityPermanentCache", m_cacheXmlPermanentEntity);

    // xml content definitions cache
    Map<String, CmsXmlContentDefinition> contentDefinitionsCache = CmsCollectionsGenericWrapper
            .createLRUMap(64);
    m_cacheContentDefinitions = Collections.synchronizedMap(contentDefinitionsCache);
    register(CmsXmlEntityResolver.class.getName() + ".contentDefinitionsCache", m_cacheContentDefinitions);

    // lock cache
    Map<String, CmsLock> lockCache = new HashMap<String, CmsLock>();
    m_cacheLock = Collections.synchronizedMap(lockCache);
    register(CmsLockManager.class.getName(), lockCache);

    // locale cache
    Map<String, Locale> map = new HashMap<String, Locale>();
    m_cacheLocale = Collections.synchronizedMap(map);
    register(CmsLocaleManager.class.getName(), map);

    // permissions cache
    Map<String, I_CmsPermissionHandler.CmsPermissionCheckResult> lruPermissions = CmsCollectionsGenericWrapper
            .createLRUMap(cacheSettings.getPermissionCacheSize());
    m_cachePermission = Collections.synchronizedMap(lruPermissions);
    register(CmsSecurityManager.class.getName(), lruPermissions);

    // user cache
    Map<String, CmsUser> lruUsers = CmsCollectionsGenericWrapper.createLRUMap(cacheSettings.getUserCacheSize());
    m_cacheUser = Collections.synchronizedMap(lruUsers);
    register(CmsDriverManager.class.getName() + ".userCache", lruUsers);

    // user list cache
    Map<String, List<CmsUser>> lruUserList = CmsCollectionsGenericWrapper
            .createLRUMap(cacheSettings.getUserCacheSize());
    m_cacheUserList = Collections.synchronizedMap(lruUserList);
    register(CmsDriverManager.class.getName() + ".userListCache", lruUserList);

    // group cache
    Map<String, CmsGroup> lruGroup = CmsCollectionsGenericWrapper
            .createLRUMap(cacheSettings.getGroupCacheSize());
    m_cacheGroup = Collections.synchronizedMap(lruGroup);
    register(CmsDriverManager.class.getName() + ".groupCache", lruGroup);

    // organizational unit cache
    Map<String, CmsOrganizationalUnit> lruOrgUnit = CmsCollectionsGenericWrapper
            .createLRUMap(cacheSettings.getOrgUnitCacheSize());
    m_cacheOrgUnit = Collections.synchronizedMap(lruOrgUnit);
    register(CmsDriverManager.class.getName() + ".orgUnitCache", lruOrgUnit);

    // user groups list cache
    Map<String, List<CmsGroup>> lruUserGroups = CmsCollectionsGenericWrapper
            .createLRUMap(cacheSettings.getUserGroupsCacheSize());
    m_cacheUserGroups = Collections.synchronizedMap(lruUserGroups);
    register(CmsDriverManager.class.getName() + ".userGroupsCache", lruUserGroups);

    // project cache
    Map<String, CmsProject> lruProjects = CmsCollectionsGenericWrapper
            .createLRUMap(cacheSettings.getProjectCacheSize());
    m_cacheProject = Collections.synchronizedMap(lruProjects);
    register(CmsDriverManager.class.getName() + ".projectCache", lruProjects);

    // project resources cache cache
    Map<String, List<CmsResource>> lruProjectResources = CmsCollectionsGenericWrapper
            .createLRUMap(cacheSettings.getProjectResourcesCacheSize());
    m_cacheProjectResources = Collections.synchronizedMap(lruProjectResources);
    register(CmsDriverManager.class.getName() + ".projectResourcesCache", lruProjectResources);

    // publish history
    int size = configuration.getPublishManager().getPublishHistorySize();
    Buffer buffer = CmsPublishHistory.getQueue(size);
    m_publishHistory = SynchronizedBuffer.decorate(buffer);
    register(CmsPublishHistory.class.getName() + ".publishHistory", buffer);

    // publish queue
    buffer = CmsPublishQueue.getQueue();
    m_publishQueue = SynchronizedBuffer.decorate(buffer);
    register(CmsPublishQueue.class.getName() + ".publishQueue", buffer);

    // resource cache
    Map<String, CmsResource> lruResources = CmsCollectionsGenericWrapper
            .createLRUMap(cacheSettings.getResourceCacheSize());
    m_cacheResource = Collections.synchronizedMap(lruResources);
    register(CmsDriverManager.class.getName() + ".resourceCache", lruResources);

    // roles cache
    Map<String, Boolean> lruHasRoles = CmsCollectionsGenericWrapper
            .createLRUMap(cacheSettings.getRolesCacheSize());
    m_cacheHasRoles = Collections.synchronizedMap(lruHasRoles);
    register(CmsDriverManager.class.getName() + ".rolesCache", lruHasRoles);

    // role lists cache
    Map<String, List<CmsRole>> lruRoleLists = CmsCollectionsGenericWrapper
            .createLRUMap(cacheSettings.getRolesCacheSize());
    m_cacheRoleLists = Collections.synchronizedMap(lruRoleLists);
    register(CmsDriverManager.class.getName() + ".roleListsCache", lruRoleLists);

    // resource list cache
    Map<String, List<CmsResource>> lruResourceList = CmsCollectionsGenericWrapper
            .createLRUMap(cacheSettings.getResourcelistCacheSize());
    m_cacheResourceList = Collections.synchronizedMap(lruResourceList);
    register(CmsDriverManager.class.getName() + ".resourceListCache", lruResourceList);

    // property cache
    Map<String, CmsProperty> lruProperty = CmsCollectionsGenericWrapper
            .createLRUMap(cacheSettings.getPropertyCacheSize());
    m_cacheProperty = Collections.synchronizedMap(lruProperty);
    register(CmsDriverManager.class.getName() + ".propertyCache", lruProperty);

    // property list cache
    Map<String, List<CmsProperty>> lruPropertyList = CmsCollectionsGenericWrapper
            .createLRUMap(cacheSettings.getPropertyListsCacheSize());
    m_cachePropertyList = Collections.synchronizedMap(lruPropertyList);
    register(CmsDriverManager.class.getName() + ".propertyListCache", lruPropertyList);

    // published resources list cache
    Map<String, List<CmsPublishedResource>> lruPublishedResources = CmsCollectionsGenericWrapper
            .createLRUMap(5);
    m_cachePublishedResources = Collections.synchronizedMap(lruPublishedResources);
    register(CmsDriverManager.class.getName() + ".publishedResourcesCache", lruPublishedResources);

    // acl cache
    Map<String, CmsAccessControlList> lruAcl = CmsCollectionsGenericWrapper
            .createLRUMap(cacheSettings.getAclCacheSize());
    m_cacheAccessControlList = Collections.synchronizedMap(lruAcl);
    register(CmsDriverManager.class.getName() + ".accessControlListCache", lruAcl);

    // vfs object cache
    Map<String, Object> vfsObjectCache = new HashMap<String, Object>();
    m_cacheVfsObject = Collections.synchronizedMap(vfsObjectCache);
    register(CmsVfsMemoryObjectCache.class.getName(), vfsObjectCache);

    // memory object cache
    Map<String, Object> memObjectCache = new HashMap<String, Object>();
    m_cacheMemObject = Collections.synchronizedMap(memObjectCache);
    register(CmsMemoryObjectCache.class.getName(), memObjectCache);

    if (LOG.isDebugEnabled()) {
        // this will happen only once during system startup
        LOG.debug(Messages.get().getBundle().key(Messages.LOG_MM_CREATED_1,
                new Date(System.currentTimeMillis())));
    }
}

From source file:com.twitter.distributedlog.BKLogHandler.java

private void asyncGetLedgerListInternal(final Comparator<LogSegmentMetadata> comparator,
        final LogSegmentFilter segmentFilter, final Watcher watcher,
        final GenericCallback<List<LogSegmentMetadata>> finalCallback, final AtomicInteger numAttemptsLeft,
        final AtomicLong backoffMillis) {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    try {/*from w  w  w.  j  av  a 2 s.c o  m*/
        if (LOG.isTraceEnabled()) {
            LOG.trace("Async getting ledger list for {}.", getFullyQualifiedName());
        }
        final GenericCallback<List<LogSegmentMetadata>> callback = new GenericCallback<List<LogSegmentMetadata>>() {
            @Override
            public void operationComplete(int rc, List<LogSegmentMetadata> result) {
                long elapsedMicros = stopwatch.stop().elapsed(TimeUnit.MICROSECONDS);
                if (KeeperException.Code.OK.intValue() != rc) {
                    getListStat.registerFailedEvent(elapsedMicros);
                } else {
                    if (LogSegmentFilter.DEFAULT_FILTER == segmentFilter) {
                        isFullListFetched.set(true);
                    }
                    getListStat.registerSuccessfulEvent(elapsedMicros);
                }
                finalCallback.operationComplete(rc, result);
            }
        };
        zooKeeperClient.get().getChildren(logMetadata.getLogSegmentsPath(), watcher,
                new AsyncCallback.Children2Callback() {
                    @Override
                    public void processResult(final int rc, final String path, final Object ctx,
                            final List<String> children, final Stat stat) {
                        if (KeeperException.Code.OK.intValue() != rc) {

                            if ((KeeperException.Code.CONNECTIONLOSS.intValue() == rc
                                    || KeeperException.Code.SESSIONEXPIRED.intValue() == rc
                                    || KeeperException.Code.SESSIONMOVED.intValue() == rc)
                                    && numAttemptsLeft.decrementAndGet() > 0) {
                                long backoffMs = backoffMillis.get();
                                backoffMillis.set(Math.min(conf.getZKRetryBackoffMaxMillis(), 2 * backoffMs));
                                scheduler.schedule(new Runnable() {
                                    @Override
                                    public void run() {
                                        asyncGetLedgerListInternal(comparator, segmentFilter, watcher,
                                                finalCallback, numAttemptsLeft, backoffMillis);
                                    }
                                }, backoffMs, TimeUnit.MILLISECONDS);
                                return;
                            }
                            callback.operationComplete(rc, null);
                            return;
                        }

                        if (LOG.isTraceEnabled()) {
                            LOG.trace("Got ledger list from {} : {}", logMetadata.getLogSegmentsPath(),
                                    children);
                        }

                        ledgerListWatchSet.set(true);
                        Set<String> segmentsReceived = new HashSet<String>();
                        segmentsReceived.addAll(segmentFilter.filter(children));
                        Set<String> segmentsAdded;
                        final Set<String> removedSegments = Collections.synchronizedSet(new HashSet<String>());
                        final Map<String, LogSegmentMetadata> addedSegments = Collections
                                .synchronizedMap(new HashMap<String, LogSegmentMetadata>());
                        Pair<Set<String>, Set<String>> segmentChanges = logSegmentCache.diff(segmentsReceived);
                        segmentsAdded = segmentChanges.getLeft();
                        removedSegments.addAll(segmentChanges.getRight());

                        if (segmentsAdded.isEmpty()) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("No segments added for {}.", getFullyQualifiedName());
                            }

                            // update the cache before fetch
                            logSegmentCache.update(removedSegments, addedSegments);

                            List<LogSegmentMetadata> segmentList;
                            try {
                                segmentList = getCachedLogSegments(comparator);
                            } catch (UnexpectedException e) {
                                callback.operationComplete(KeeperException.Code.DATAINCONSISTENCY.intValue(),
                                        null);
                                return;
                            }
                            callback.operationComplete(KeeperException.Code.OK.intValue(), segmentList);
                            notifyUpdatedLogSegments(segmentList);
                            if (!removedSegments.isEmpty()) {
                                notifyOnOperationComplete();
                            }
                            return;
                        }

                        final AtomicInteger numChildren = new AtomicInteger(segmentsAdded.size());
                        final AtomicInteger numFailures = new AtomicInteger(0);
                        for (final String segment : segmentsAdded) {
                            metadataStore.getLogSegment(logMetadata.getLogSegmentPath(segment))
                                    .addEventListener(new FutureEventListener<LogSegmentMetadata>() {

                                        @Override
                                        public void onSuccess(LogSegmentMetadata result) {
                                            addedSegments.put(segment, result);
                                            complete();
                                        }

                                        @Override
                                        public void onFailure(Throwable cause) {
                                            // NONODE exception is possible in two cases
                                            // 1. A log segment was deleted by truncation between the call to getChildren and read
                                            // attempt on the znode corresponding to the segment
                                            // 2. In progress segment has been completed => inprogress ZNode does not exist
                                            if (cause instanceof KeeperException
                                                    && KeeperException.Code.NONODE == ((KeeperException) cause)
                                                            .code()) {
                                                removedSegments.add(segment);
                                                complete();
                                            } else {
                                                // fail fast
                                                if (1 == numFailures.incrementAndGet()) {
                                                    int rcToReturn = KeeperException.Code.SYSTEMERROR
                                                            .intValue();
                                                    if (cause instanceof KeeperException) {
                                                        rcToReturn = ((KeeperException) cause).code()
                                                                .intValue();
                                                    } else if (cause instanceof ZKException) {
                                                        rcToReturn = ((ZKException) cause)
                                                                .getKeeperExceptionCode().intValue();
                                                    }
                                                    // :( properly we need dlog related response code.
                                                    callback.operationComplete(rcToReturn, null);
                                                    return;
                                                }
                                            }
                                        }

                                        private void complete() {
                                            if (0 == numChildren.decrementAndGet() && numFailures.get() == 0) {
                                                // update the cache only when fetch completed
                                                logSegmentCache.update(removedSegments, addedSegments);
                                                List<LogSegmentMetadata> segmentList;
                                                try {
                                                    segmentList = getCachedLogSegments(comparator);
                                                } catch (UnexpectedException e) {
                                                    callback.operationComplete(
                                                            KeeperException.Code.DATAINCONSISTENCY.intValue(),
                                                            null);
                                                    return;
                                                }
                                                callback.operationComplete(KeeperException.Code.OK.intValue(),
                                                        segmentList);
                                                notifyUpdatedLogSegments(segmentList);
                                                notifyOnOperationComplete();
                                            }
                                        }
                                    });
                        }
                    }
                }, null);
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        getListStat.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
        finalCallback.operationComplete(KeeperException.Code.CONNECTIONLOSS.intValue(), null);
    } catch (InterruptedException e) {
        getListStat.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
        finalCallback.operationComplete(KeeperException.Code.CONNECTIONLOSS.intValue(), null);
    }
}

From source file:de.tum.bgu.msm.syntheticPopulationGenerator.capeTown.SyntheticPopCT.java

private void runIPUbyCityAndCounty() {
    //IPU process for independent municipalities (only household attributes)
    logger.info("   Starting to prepare the data for IPU");

    //Read the frequency matrix
    int[] microDataIds = frequencyMatrix.getColumnAsInt("ID");
    frequencyMatrix.buildIndex(frequencyMatrix.getColumnPosition("ID"));

    //Create the weights table (for all the municipalities)
    TableDataSet weightsMatrix = new TableDataSet();
    weightsMatrix.appendColumn(microDataIds, "ID");

    //Create errors by county
    TableDataSet errorsCounty = new TableDataSet();
    TableDataSet errorsMunicipality = new TableDataSet();
    TableDataSet errorsSummary = new TableDataSet();
    String[] labels = new String[] { "error", "iterations", "time" };
    errorsCounty = initializeErrors(errorsCounty, attributesCounty, countyID);
    errorsMunicipality = initializeErrors(errorsMunicipality, attributesMunicipality, cityID);
    errorsSummary = initializeErrors(errorsSummary, labels, countyID);

    //For each county---------------------------------------------

    for (int county : counties) {

        long startTime = System.nanoTime();
        municipalities = municipalitiesByCounty.get(county);

        //weights, values, control totals
        Map<Integer, double[]> weightsByMun = Collections.synchronizedMap(new HashMap<>());
        Map<Integer, double[]> minWeightsByMun = Collections.synchronizedMap(new HashMap<>());
        Map<String, int[]> valuesByHousehold = Collections.synchronizedMap(new HashMap<>());
        Map<String, Integer> totalCounty = Collections.synchronizedMap(new HashMap<>());
        Map<Integer, Map<String, Integer>> totalMunicipality = Collections.synchronizedMap(new HashMap<>());
        Map<Integer, Map<String, Double>> errorByMun = Collections.synchronizedMap(new HashMap<>());
        Map<String, Double> errorByRegion = Collections.synchronizedMap(new HashMap<>());
        double weightedSum0 = 0f;

        //parameters of the IPU
        int finish = 0;
        int iteration = 0;
        double maxError = 0.00001;
        int maxIterations = 500;
        double minError = 100000;
        double initialError = 10000;
        double improvementError = 0.001;
        double iterationError = 2;
        double increaseError = 1.05;

        //initialize errors, considering the first weight (equal to 1)
        for (String attribute : attributesCounty) {
            int[] values = new int[frequencyMatrix.getRowCount()];
            for (int i = 1; i <= frequencyMatrix.getRowCount(); i++) {
                values[i - 1] = (int) frequencyMatrix.getValueAt(i, attribute);
                if (attribute.equals(attributesCounty[0])) {
                    weightedSum0 = weightedSum0 + values[i - 1] * municipalities.size();
                }/*  w  w  w  . j  a  v a 2 s  .  co  m*/
            }
            valuesByHousehold.put(attribute, values);
            int total = (int) marginalsCounty.getIndexedValueAt(county, attribute);
            totalCounty.put(attribute, total);
            errorByRegion.put(attribute, 0.);
        }
        for (String attribute : attributesMunicipality) {
            int[] values = new int[frequencyMatrix.getRowCount()];
            for (int i = 1; i <= frequencyMatrix.getRowCount(); i++) {
                values[i - 1] = (int) frequencyMatrix.getValueAt(i, attribute);
            }
            valuesByHousehold.put(attribute, values);
            Iterator<Integer> iterator = municipalities.iterator();
            while (iterator.hasNext()) {
                Integer municipality = iterator.next();
                double[] dummy = SiloUtil.createArrayWithValue(frequencyMatrix.getRowCount(), 1.);
                weightsByMun.put(municipality, dummy);
                double[] dummy1 = SiloUtil.createArrayWithValue(frequencyMatrix.getRowCount(), 1.);
                minWeightsByMun.put(municipality, dummy1);
                if (totalMunicipality.containsKey(municipality)) {
                    Map<String, Integer> innerMap = totalMunicipality.get(municipality);
                    innerMap.put(attribute,
                            (int) marginalsMunicipality.getIndexedValueAt(municipality, attribute));
                    totalMunicipality.put(municipality, innerMap);
                    Map<String, Double> inner1 = errorByMun.get(municipality);
                    inner1.put(attribute, 0.);
                    errorByMun.put(municipality, inner1);
                } else {
                    HashMap<String, Integer> inner = new HashMap<>();
                    inner.put(attribute,
                            (int) marginalsMunicipality.getIndexedValueAt(municipality, attribute));
                    totalMunicipality.put(municipality, inner);
                    HashMap<String, Double> inner1 = new HashMap<>();
                    inner1.put(attribute, 0.);
                    errorByMun.put(municipality, inner1);
                }
            }
        }

        //for each iteration
        while (finish == 0 & iteration < maxIterations) {

            //For each municipality, obtain the weight matching each attribute
            ConcurrentExecutor executor = ConcurrentExecutor.cachedService();
            Iterator<Integer> iterator = municipalities.iterator();
            while (iterator.hasNext()) {
                Integer municipality = iterator.next();
                executor.addTaskToQueue(() -> {
                    for (String attribute : attributesMunicipality) {
                        double weightedSumMunicipality = SiloUtil.sumProduct(weightsByMun.get(municipality),
                                valuesByHousehold.get(attribute));
                        if (weightedSumMunicipality > 0.001) {
                            double updatingFactor = totalMunicipality.get(municipality).get(attribute)
                                    / weightedSumMunicipality;
                            double[] previousWeights = weightsByMun.get(municipality);
                            int[] values = valuesByHousehold.get(attribute);
                            double[] updatedWeights = new double[previousWeights.length];
                            IntStream.range(0, previousWeights.length).parallel()
                                    .forEach(id -> updatedWeights[id] = multiplyIfNotZero(previousWeights[id],
                                            values[id], updatingFactor));
                            weightsByMun.put(municipality, updatedWeights);
                        }
                    }
                    return null;
                });
            }
            executor.execute();

            //For each attribute at the region level (landkreise), we obtain the weights
            double weightedSumRegion = 0;
            for (String attribute : attributesCounty) {
                Iterator<Integer> iterator1 = municipalities.iterator();
                while (iterator1.hasNext()) {
                    Integer municipality = iterator1.next();
                    weightedSumRegion = weightedSumRegion + SiloUtil.sumProduct(weightsByMun.get(municipality),
                            valuesByHousehold.get(attribute));
                }
                if (weightedSumRegion > 0.001) {
                    double updatingFactor = totalCounty.get(attribute) / weightedSumRegion;
                    Iterator<Integer> iterator2 = municipalities.iterator();
                    while (iterator2.hasNext()) {
                        Integer municipality = iterator2.next();
                        double[] previousWeights = weightsByMun.get(municipality);
                        int[] values = valuesByHousehold.get(attribute);
                        double[] updatedWeights = new double[previousWeights.length];
                        IntStream.range(0, previousWeights.length).parallel()
                                .forEach(id -> updatedWeights[id] = multiplyIfNotZero(previousWeights[id],
                                        values[id], updatingFactor));
                        weightsByMun.put(municipality, updatedWeights);
                    }
                }
                //logger.info("Attribute " + attribute + ": sum is " + weightedSumRegion);
                weightedSumRegion = 0;
            }

            //obtain the errors by municipality
            double averageErrorIteration = 0.;
            int counter = 0;
            ConcurrentExecutor executor1 = ConcurrentExecutor.cachedService();
            Iterator<Integer> iterator1 = municipalities.iterator();
            while (iterator1.hasNext()) {
                Integer municipality = iterator1.next();
                Map<String, Double> errorsByMunicipality = Collections.synchronizedMap(new HashMap<>());
                executor1.addTaskToQueue(() -> {
                    for (String attribute : attributesMunicipality) {
                        double weightedSumMunicipality = SiloUtil.sumProduct(weightsByMun.get(municipality),
                                valuesByHousehold.get(attribute));
                        double errorByAttributeAndMunicipality = 0;
                        if (totalMunicipality.get(municipality).get(attribute) > 0) {
                            errorByAttributeAndMunicipality = Math.abs((weightedSumMunicipality
                                    - totalMunicipality.get(municipality).get(attribute))
                                    / totalMunicipality.get(municipality).get(attribute));
                            errorsByMunicipality.put(attribute, errorByAttributeAndMunicipality);
                        }
                    }
                    return null;
                });
                errorByMun.put(municipality, errorsByMunicipality);
            }
            executor1.execute();
            for (int municipality : municipalities) {
                averageErrorIteration = averageErrorIteration
                        + errorByMun.get(municipality).values().stream().mapToDouble(Number::doubleValue).sum();
                counter = counter + errorByMun.get(municipality).entrySet().size();
            }

            //obtain errors by county
            for (String attributeC : attributesCounty) {
                double errorByCounty = 0.;
                double weightedSumCounty = 0.;
                if (totalCounty.get(attributeC) > 0) {
                    Iterator<Integer> iterator3 = municipalities.iterator();
                    while (iterator3.hasNext()) {
                        Integer municipality = iterator3.next();
                        double weightedSum = SiloUtil.sumProduct(weightsByMun.get(municipality),
                                valuesByHousehold.get(attributeC));
                        weightedSumCounty += weightedSum;
                    }
                    errorByCounty = errorByCounty + Math.abs(
                            (weightedSumCounty - totalCounty.get(attributeC)) / totalCounty.get(attributeC));
                    errorByRegion.put(attributeC, errorByCounty);
                    averageErrorIteration += errorByCounty;
                    counter++;
                }
            }

            averageErrorIteration = averageErrorIteration / counter;
            logger.info("   County " + county + ". Iteration " + iteration + ". Average error: "
                    + averageErrorIteration * 100 + " %.");

            //Stopping criteria: exceeds the maximum number of iterations or the maximum error is lower than the threshold
            if (averageErrorIteration < maxError) {
                finish = 1;
                logger.info("   IPU finished after :" + iteration
                        + " iterations with a minimum average error of: " + minError * 100 + " %.");
                iteration = maxIterations + 1;
            } else if ((iteration / iterationError) % 1 == 0) {
                if (Math.abs((initialError - averageErrorIteration) / initialError) < improvementError) {
                    finish = 1;
                    logger.info("   IPU finished after " + iteration
                            + " iterations because the error does not improve. The minimum average error is: "
                            + minError * 100 + " %.");
                } else if (averageErrorIteration == 0) {
                    finish = 1;
                    logger.info("   IPU finished after " + iteration
                            + " iterations because the error starts increasing. The minimum average error is: "
                            + minError * 100 + " %.");
                } else {
                    initialError = averageErrorIteration;
                    iteration = iteration + 1;
                }
            } else if (iteration == maxIterations) {
                finish = 1;
                logger.info(
                        "   IPU finished after the total number of iterations. The minimum average error is: "
                                + minError * 100 + " %.");
            } else {
                iteration = iteration + 1;
            }

            if (averageErrorIteration < minError) {
                for (int municipality : municipalities) {
                    double[] minW = weightsByMun.get(municipality);
                    minWeightsByMun.put(municipality, minW);
                }
                minError = averageErrorIteration;
            }
            long estimatedTime = (System.nanoTime() - startTime) / 1000000000;
            errorsSummary.setIndexedValueAt(county, "error", (float) minError);
            errorsSummary.setIndexedValueAt(county, "iterations", iteration);
            errorsSummary.setIndexedValueAt(county, "time", estimatedTime);
        }

        //Write the weights after finishing IPU for each municipality (saved each time over the previous version)
        for (int municipality : municipalities) {
            weightsMatrix.appendColumn(minWeightsByMun.get(municipality), Integer.toString(municipality));
        }

        SiloUtil.writeTableDataSet(weightsMatrix, "input/syntheticPopulation/weights1.csv");

        //Copy the errors per attribute
        for (String attribute : attributesCounty) {
            errorsCounty.setIndexedValueAt(county, attribute, errorByRegion.get(attribute).floatValue());
        }
        for (int municipality : municipalities) {
            for (String attribute : attributesMunicipality) {
                if (totalMunicipality.get(municipality).get(attribute) > 0) {
                    errorsMunicipality.setIndexedValueAt(municipality, attribute,
                            errorByMun.get(municipality).get(attribute).floatValue());
                }
            }
        }

        //Write the weights after finishing IPU for each county
        SiloUtil.writeTableDataSet(weightsMatrix, rb.getString(PROPERTIES_WEIGHTS_MATRIX));
        SiloUtil.writeTableDataSet(errorsMunicipality, "microData/interimFiles/errorsHouseholdIPU.csv");
        SiloUtil.writeTableDataSet(errorsCounty, "microData/interimFiles/errorsRegionIPU.csv");
        SiloUtil.writeTableDataSet(errorsSummary, "microData/interimFiles/summaryIPU.csv");
    }
    //Write the weights final table
    weightsTable = weightsMatrix;
    weightsTable.buildIndex(weightsTable.getColumnPosition("ID"));

    logger.info("   IPU finished");
}

From source file:org.opencms.staticexport.CmsStaticExportManager.java

/**
 * Initializes the static export manager with the OpenCms system configuration.<p>
 * //w ww  .  j  av a 2  s.co m
 * @param cms an OpenCms context object
 */
public void initialize(CmsObject cms) {

    // initialize static export RFS path (relative to web application)
    m_staticExportPath = normalizeExportPath(m_staticExportPathConfigured);
    m_staticExportWorkPath = normalizeExportPath(getExportWorkPathForConfiguration());
    if (m_staticExportPath.equals(OpenCms.getSystemInfo().getWebApplicationRfsPath())) {
        throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_INVALID_EXPORT_PATH_0));
    }
    // initialize prefix variables
    m_rfsPrefix = normalizeRfsPrefix(m_rfsPrefixConfigured);
    Iterator<CmsStaticExportRfsRule> itRfsRules = m_rfsRules.iterator();
    while (itRfsRules.hasNext()) {
        CmsStaticExportRfsRule rule = itRfsRules.next();
        try {
            rule.setExportPath(normalizeExportPath(rule.getExportPathConfigured()));
        } catch (CmsIllegalArgumentException e) {
            CmsLog.INIT.warn(e.getMessageContainer());
            rule.setExportPath(m_staticExportPath);
        }
        try {
            rule.setExportWorkPath(normalizeExportPath(rule.getExportWorkPathConfigured()));
        } catch (CmsIllegalArgumentException e) {
            CmsLog.INIT.warn(e.getMessageContainer());
            rule.setExportWorkPath(m_staticExportWorkPath);
        }
        rule.setRfsPrefix(normalizeRfsPrefix(rule.getRfsPrefixConfigured()));
    }
    m_vfsPrefix = insertContextStrings(m_vfsPrefixConfigured);
    m_vfsPrefix = CmsFileUtil.normalizePath(m_vfsPrefix, '/');
    if (CmsResource.isFolder(m_vfsPrefix)) {
        // ensure prefix does NOT end with a folder '/'
        m_vfsPrefix = m_vfsPrefix.substring(0, m_vfsPrefix.length() - 1);
    }
    if (CmsLog.INIT.isDebugEnabled()) {
        if (cms != null) {
            CmsLog.INIT.debug(Messages.get().getBundle().key(Messages.INIT_SE_MANAGER_CREATED_1, cms));
        } else {
            CmsLog.INIT.debug(Messages.get().getBundle().key(Messages.INIT_SE_MANAGER_CREATED_0));
        }
    }

    Map<String, String> lruMap1 = CmsCollectionsGenericWrapper.createLRUMap(2048);
    m_cacheOnlineLinks = Collections.synchronizedMap(lruMap1);
    // map must be of type "LRUMap" so that memory monitor can acecss all information
    OpenCms.getMemoryMonitor().register(this.getClass().getName() + ".m_cacheOnlineLinks", lruMap1);

    Map<String, CmsStaticExportData> lruMap2 = CmsCollectionsGenericWrapper.createLRUMap(2048);
    m_cacheExportUris = Collections.synchronizedMap(lruMap2);
    // map must be of type "LRUMap" so that memory monitor can acecss all information
    OpenCms.getMemoryMonitor().register(this.getClass().getName() + ".m_cacheExportUris", lruMap2);

    Map<String, Boolean> lruMap3 = CmsCollectionsGenericWrapper.createLRUMap(2048);
    m_cacheSecureLinks = Collections.synchronizedMap(lruMap3);
    // map must be of type "LRUMap" so that memory monitor can acecss all information
    OpenCms.getMemoryMonitor().register(this.getClass().getName() + ".m_cacheSecureLinks", lruMap3);

    Map<String, Boolean> lruMap4 = CmsCollectionsGenericWrapper.createLRUMap(2048);
    m_cacheExportLinks = Collections.synchronizedMap(lruMap4);
    // map must be of type "LRUMap" so that memory monitor can acecss all information
    OpenCms.getMemoryMonitor().register(this.getClass().getName() + ".m_cacheExportLinks", lruMap4);

    // register this object as event listener
    OpenCms.addCmsEventListener(this, new int[] { I_CmsEventListener.EVENT_PUBLISH_PROJECT,
            I_CmsEventListener.EVENT_CLEAR_CACHES, I_CmsEventListener.EVENT_UPDATE_EXPORTS });

    m_exportFolderMatcher = new CmsExportFolderMatcher(m_exportFolders, m_testResource);

    // get the default accept-language header value
    m_defaultAcceptLanguageHeader = CmsAcceptLanguageHeaderParser.createLanguageHeader();

    // get the default accept-charset header value
    m_defaultAcceptCharsetHeader = OpenCms.getSystemInfo().getDefaultEncoding();

    // get the export url prefix
    int pos = m_exportUrl.indexOf("://");
    if (pos > 0) {
        // absolute link, remove http://servername
        int pos2 = m_exportUrl.indexOf('/', pos + 3);
        if (pos2 > 0) {
            m_exportUrlPrefix = m_exportUrl.substring(pos2);
        } else {
            // should never happen
            m_exportUrlPrefix = "";
        }
    } else {
        m_exportUrlPrefix = m_exportUrl;
    }
    if (CmsLog.INIT.isInfoEnabled()) {
        if (isStaticExportEnabled()) {
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_STATIC_EXPORT_ENABLED_0));
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_DEFAULT_1,
                    Boolean.valueOf(getExportPropertyDefault())));
            itRfsRules = m_rfsRules.iterator();
            while (itRfsRules.hasNext()) {
                CmsStaticExportRfsRule rfsRule = itRfsRules.next();
                CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_RFS_RULE_EXPORT_PATH_2,
                        rfsRule.getSource(), rfsRule.getExportPath()));
                CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_RFS_RULE_RFS_PREFIX_2,
                        rfsRule.getSource(), rfsRule.getRfsPrefix()));
                if (rfsRule.getUseRelativeLinks() != null) {
                    if (rfsRule.getUseRelativeLinks().booleanValue()) {
                        CmsLog.INIT.info(Messages.get().getBundle()
                                .key(Messages.INIT_EXPORT_RFS_RULE_RELATIVE_LINKS_1, rfsRule.getSource()));
                    } else {
                        CmsLog.INIT.info(Messages.get().getBundle()
                                .key(Messages.INIT_EXPORT_RFS_RULE_ABSOLUTE_LINKS_1, rfsRule.getSource()));
                    }
                }
            }
            // default rule
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_RFS_RULE_EXPORT_PATH_2, "/",
                    m_staticExportPath));
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_RFS_RULE_RFS_PREFIX_2, "/",
                    m_rfsPrefix));
            if (m_exportRelativeLinks) {
                CmsLog.INIT.info(
                        Messages.get().getBundle().key(Messages.INIT_EXPORT_RFS_RULE_RELATIVE_LINKS_1, "/"));
            } else {
                CmsLog.INIT.info(
                        Messages.get().getBundle().key(Messages.INIT_EXPORT_RFS_RULE_ABSOLUTE_LINKS_1, "/"));
            }
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_VFS_PREFIX_1, getVfsPrefix()));
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_EXPORT_HANDLER_1,
                    getHandler().getClass().getName()));
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_URL_1, getExportUrl()));
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_EXPORT_OPTIMIZATION_1,
                    getPlainExportOptimization()));
            CmsLog.INIT.info(
                    Messages.get().getBundle().key(Messages.INIT_EXPORT_TESTRESOURCE_1, getTestResource()));
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_LINKSUBSTITUTION_HANDLER_1,
                    getLinkSubstitutionHandler().getClass().getName()));
        } else {
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_STATIC_EXPORT_DISABLED_0));
        }
    }
}

From source file:org.apache.tapestry.engine.AbstractEngine.java

/**
 *  Creates the shared Global object.  This implementation looks for an configuration
 *  property, <code>org.apache.tapestry.global-class</code>, and instantiates that class
 *  using a no-arguments/*w  ww  . ja  v  a2s  .c  om*/
 *  constructor.  If the property is not defined, a synchronized
 *  {@link java.util.HashMap} is created.
 *
 *  @since 2.3
 *
 **/

protected Object createGlobal(RequestContext context) {
    String className = _propertySource.getPropertyValue("org.apache.tapestry.global-class");

    if (className == null)
        return Collections.synchronizedMap(new HashMap());

    Class globalClass = _resolver.findClass(className);

    try {
        return globalClass.newInstance();
    } catch (Exception ex) {
        throw new ApplicationRuntimeException(
                Tapestry.format("AbstractEngine.unable-to-instantiate-global", className), ex);
    }
}

From source file:org.apache.hadoop.hbase.client.HTable.java

/**
 * {@inheritDoc}//w w  w .  j  av a2 s .  co  m
 */
@Override
public <T extends Service, R> Map<byte[], R> coprocessorService(final Class<T> service, byte[] startKey,
        byte[] endKey, final Batch.Call<T, R> callable) throws ServiceException, Throwable {
    final Map<byte[], R> results = Collections.synchronizedMap(new TreeMap<byte[], R>(Bytes.BYTES_COMPARATOR));
    coprocessorService(service, startKey, endKey, callable, new Batch.Callback<R>() {
        public void update(byte[] region, byte[] row, R value) {
            if (region != null) {
                results.put(region, value);
            }
        }
    });
    return results;
}