Example usage for java.lang Number longValue

List of usage examples for java.lang Number longValue

Introduction

In this page you can find the example usage for java.lang Number longValue.

Prototype

public abstract long longValue();

Source Link

Document

Returns the value of the specified number as a long .

Usage

From source file:cc.tooyoung.common.db.JdbcTemplate.java

public long queryForLong(String sql, Object[] args, int[] argTypes) throws DataAccessException {
    Number number = (Number) queryForObject(sql, args, argTypes, Long.class);
    return (number != null ? number.longValue() : 0);
}

From source file:org.apache.calcite.runtime.SqlFunctions.java

public static BigDecimal toBigDecimal(Number number) {
    // There are some values of "long" that cannot be represented as "double".
    // Not so "int". If it isn't a long, go straight to double.
    return number instanceof BigDecimal ? (BigDecimal) number
            : number instanceof BigInteger ? new BigDecimal((BigInteger) number)
                    : number instanceof Long ? new BigDecimal(number.longValue())
                            : new BigDecimal(number.doubleValue());
}

From source file:com.haulmont.cuba.core.app.RdbmsStore.java

@Override
public long getCount(LoadContext<? extends Entity> context) {
    if (log.isDebugEnabled())
        log.debug("getCount: metaClass=" + context.getMetaClass()
                + (context.getPrevQueries().isEmpty() ? "" : ", from selected") + ", query="
                + (context.getQuery() == null ? null
                        : DataServiceQueryBuilder.printQuery(context.getQuery().getQueryString())));

    MetaClass metaClass = metadata.getClassNN(context.getMetaClass());

    if (!isEntityOpPermitted(metaClass, EntityOp.READ)) {
        log.debug("reading of {} not permitted, returning 0", metaClass);
        return 0;
    }/* w ww.  ja  v  a  2  s .  co  m*/

    queryResultsManager.savePreviousQueryResults(context);

    if (security.hasInMemoryConstraints(metaClass, ConstraintOperationType.READ, ConstraintOperationType.ALL)) {
        context = context.copy();
        List resultList;
        try (Transaction tx = createLoadTransaction()) {
            EntityManager em = persistence.getEntityManager(storeName);
            em.setSoftDeletion(context.isSoftDeletion());
            persistence.getEntityManagerContext(storeName).setDbHints(context.getDbHints());

            boolean ensureDistinct = false;
            if (serverConfig.getInMemoryDistinct() && context.getQuery() != null) {
                QueryTransformer transformer = QueryTransformerFactory
                        .createTransformer(context.getQuery().getQueryString());
                ensureDistinct = transformer.removeDistinct();
                if (ensureDistinct) {
                    context.getQuery().setQueryString(transformer.getResult());
                }
            }
            context.getQuery().setFirstResult(0);
            context.getQuery().setMaxResults(0);

            Query query = createQuery(em, context, false);
            query.setView(createRestrictedView(context));

            resultList = getResultList(context, query, ensureDistinct);

            tx.commit();
        }
        return resultList.size();
    } else {
        QueryTransformer transformer = QueryTransformerFactory
                .createTransformer(context.getQuery().getQueryString());
        transformer.replaceWithCount();
        context = context.copy();
        context.getQuery().setQueryString(transformer.getResult());

        Number result;
        try (Transaction tx = createLoadTransaction()) {
            EntityManager em = persistence.getEntityManager(storeName);
            em.setSoftDeletion(context.isSoftDeletion());
            persistence.getEntityManagerContext(storeName).setDbHints(context.getDbHints());

            Query query = createQuery(em, context, false);
            result = (Number) query.getSingleResult();

            tx.commit();
        }

        return result.longValue();
    }
}

From source file:org.apache.storm.container.cgroup.CgroupManager.java

public void reserveResourcesForWorker(String workerId, Map resourcesMap) throws SecurityException {
    Number cpuNum = null;/*w w  w  . ja va 2 s  . c  o m*/
    // The manually set STORM_WORKER_CGROUP_CPU_LIMIT config on supervisor will overwrite resources assigned by RAS (Resource Aware Scheduler)
    if (this.conf.get(Config.STORM_WORKER_CGROUP_CPU_LIMIT) != null) {
        cpuNum = (Number) this.conf.get(Config.STORM_WORKER_CGROUP_CPU_LIMIT);
    } else if (resourcesMap.get("cpu") != null) {
        cpuNum = (Number) resourcesMap.get("cpu");
    }

    Number totalMem = null;
    // The manually set STORM_WORKER_CGROUP_MEMORY_MB_LIMIT config on supervisor will overwrite resources assigned by RAS (Resource Aware Scheduler)
    if (this.conf.get(Config.STORM_WORKER_CGROUP_MEMORY_MB_LIMIT) != null) {
        totalMem = (Number) this.conf.get(Config.STORM_WORKER_CGROUP_MEMORY_MB_LIMIT);
    } else if (resourcesMap.get("memory") != null) {
        totalMem = (Number) resourcesMap.get("memory");
    }

    CgroupCommon workerGroup = new CgroupCommon(workerId, this.hierarchy, this.rootCgroup);
    try {
        this.center.createCgroup(workerGroup);
    } catch (Exception e) {
        LOG.error("Error when creating Cgroup: {}", e);
    }

    if (cpuNum != null) {
        CpuCore cpuCore = (CpuCore) workerGroup.getCores().get(SubSystemType.cpu);
        try {
            cpuCore.setCpuShares(cpuNum.intValue());
        } catch (IOException e) {
            throw new RuntimeException("Cannot set cpu.shares! Exception: ", e);
        }
    }

    if (totalMem != null) {
        MemoryCore memCore = (MemoryCore) workerGroup.getCores().get(SubSystemType.memory);
        try {
            memCore.setPhysicalUsageLimit(Long.valueOf(totalMem.longValue() * 1024 * 1024));
        } catch (IOException e) {
            throw new RuntimeException("Cannot set memory.limit_in_bytes! Exception: ", e);
        }
    }
}

From source file:com.heliosapm.opentsdb.TSDBSubmitterImpl.java

/**
 * {@inheritDoc}/*from   w w  w.ja  v  a2s  .c om*/
 * @see com.heliosapm.opentsdb.TSDBSubmitter#trace(javax.management.ObjectName, java.lang.String, java.util.Map, java.lang.String[])
 */
@Override
public void trace(final ObjectName target, final String metricName, final Map<String, Object> attributeValues,
        final String... objectNameKeys) {
    if (target == null)
        throw new IllegalArgumentException("The passed target ObjectName was null");
    if (objectNameKeys == null || objectNameKeys.length == 0)
        throw new IllegalArgumentException("At least one ObjectName Key is required");
    if (attributeValues == null || attributeValues.isEmpty())
        return;
    final String m = (metricName == null || metricName.trim().isEmpty()) ? target.getDomain()
            : metricName.trim();
    final Map<String, String> tags = new LinkedHashMap<String, String>(rootTagsMap);
    int keyCount = 0;
    boolean all = (objectNameKeys.length == 1 && "*".equals(objectNameKeys[0]));
    if (all) {
        for (Map.Entry<String, String> entry : target.getKeyPropertyList().entrySet()) {
            tags.put(clean(entry.getKey()), clean(entry.getValue()));
            keyCount++;
        }
    } else {
        for (String key : objectNameKeys) {
            if (key == null || key.trim().isEmpty())
                continue;
            String v = clean(target, key.trim());
            if (v == null || v.isEmpty())
                continue;
            tags.put(clean(key), clean(v));
            keyCount++;
        }
    }
    if (keyCount == 0)
        throw new IllegalArgumentException("No ObjectName Keys Usable as Tags. Keys: "
                + Arrays.toString(objectNameKeys) + ", ObjectName: [" + target.toString() + "]");

    for (Map.Entry<String, Object> attr : attributeValues.entrySet()) {
        final String attributeName = clean(attr.getKey());
        try {
            tags.put("metric", attributeName);
            final Object v = attr.getValue();
            if (v == null)
                continue;
            if (v instanceof Number) {
                if (v instanceof Double) {
                    trace(m, (Double) v, tags);
                } else {
                    trace(m, ((Number) v).longValue(), tags);
                }
            } else if (v instanceof CompositeData) {
                final CompositeData cd = (CompositeData) v;
                tags.put("ctype", attributeName);
                try {
                    Map<String, Number> cmap = fromOpenType(cd);
                    for (Map.Entry<String, Number> ce : cmap.entrySet()) {
                        final String key = clean(ce.getKey());
                        tags.put("metric", key);
                        try {
                            final Number cv = ce.getValue();
                            if (v instanceof Double) {
                                trace(m, cv.doubleValue(), tags);
                            } else {
                                trace(m, cv.longValue(), tags);
                            }
                        } finally {
                            tags.put("metric", attributeName);
                        }
                    }
                } finally {
                    tags.remove("ctype");
                }
            }
        } finally {
            tags.remove("metric");
        }
    }
}

From source file:com.heliosapm.opentsdb.TSDBSubmitterImpl.java

/**
 * {@inheritDoc}/*from www . j av  a 2 s . c om*/
 * @see com.heliosapm.opentsdb.TSDBSubmitter#trace(java.util.Map)
 */
@Override
public void trace(final Map<ObjectName, Map<String, Object>> batchResults) {
    if (batchResults == null || batchResults.isEmpty())
        return;
    for (Map.Entry<ObjectName, Map<String, Object>> entry : batchResults.entrySet()) {
        final ObjectName on = entry.getKey();
        final Map<String, Object> keyValuePairs = entry.getValue();
        TSDBJMXResultTransformer transformer = tsdbConnection.transformCache.getTransformer(on);
        if (transformer != null) {
            Map<ObjectName, Number> transformed = transformer.transform(on, keyValuePairs);
            for (Map.Entry<ObjectName, Number> t : transformed.entrySet()) {
                final Number v = t.getValue();
                if (v == null)
                    continue;
                if (v instanceof Double) {
                    trace(t.getKey(), v.doubleValue());
                } else {
                    trace(t.getKey(), v.longValue());
                }
            }
        } else {
            for (Map.Entry<String, Object> attr : entry.getValue().entrySet()) {
                final Object v = attr.getValue();
                if (v == null)
                    continue;
                if (v instanceof Number) {
                    if (v instanceof Double) {
                        trace(JMXHelper.objectName(clean(
                                new StringBuilder(on.toString()).append(",metric=").append(attr.getKey()))),
                                ((Number) v).doubleValue());
                    } else {
                        trace(JMXHelper.objectName(clean(
                                new StringBuilder(on.toString()).append(",metric=").append(attr.getKey()))),
                                ((Number) v).longValue());
                    }
                } else if (v instanceof CompositeData) {
                    Map<ObjectName, Number> cmap = fromOpenType(on, (CompositeData) v);
                    for (Map.Entry<ObjectName, Number> ce : cmap.entrySet()) {
                        final Number cv = ce.getValue();
                        if (v instanceof Double) {
                            trace(ce.getKey(), cv.doubleValue());
                        } else {
                            trace(ce.getKey(), cv.longValue());
                        }
                    }
                }
            }
        }
    }
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.NodeHelper.java

public static Number cast(final Number number, final AbstractInsnNode numberNode,
        final AbstractInsnNode castNode) {
    if (castNode == null) {
        return null;
    }/*from   ww  w  .  j a va2 s. c o m*/
    if (castNode == numberNode) {
        return number;
    }
    final int opcode = castNode.getOpcode();
    if (opcode == I2B) {
        return (byte) number.intValue();
    }
    // if(opcode==I2C) {
    // return (char)number.intValue();
    // }
    if (opcode == I2L) {
        return (long) number.intValue();
    }
    if (opcode == I2S) {
        return (short) number.intValue();
    }
    if (opcode == I2D) {
        return (double) number.intValue();
    }
    if (opcode == I2F) {
        return (float) number.intValue();
    }
    if (opcode == D2F) {
        return (float) number.doubleValue();
    }
    if (opcode == D2I) {
        return (int) number.doubleValue();
    }
    if (opcode == D2L) {
        return (long) number.doubleValue();
    }
    if (opcode == F2D) {
        return (double) number.floatValue();
    }
    if (opcode == F2I) {
        return (int) number.floatValue();
    }
    if (opcode == F2L) {
        return (long) number.floatValue();
    }
    if (opcode == L2D) {
        return (long) number.longValue();
    }
    if (opcode == L2F) {
        return (float) number.longValue();
    }
    if (opcode == L2I) {
        return (int) number.longValue();
    }

    return number;
}

From source file:lib.JdbcTemplate.java

@Override
@Deprecated/*from  w ww  . j a  va 2 s  .c o m*/
public long queryForLong(String sql) throws DataAccessException {
    Number number = queryForObject(sql, Long.class);
    return (number != null ? number.longValue() : 0);
}

From source file:lib.JdbcTemplate.java

@Override
@Deprecated//from  ww w . jav a  2 s.c o  m
public long queryForLong(String sql, Object... args) throws DataAccessException {
    Number number = queryForObject(sql, args, Long.class);
    return (number != null ? number.longValue() : 0);
}

From source file:lib.JdbcTemplate.java

@Override
@Deprecated/*from w  w w  .ja va  2  s . c  o m*/
public long queryForLong(String sql, Object[] args, int[] argTypes) throws DataAccessException {
    Number number = queryForObject(sql, args, argTypes, Long.class);
    return (number != null ? number.longValue() : 0);
}