Example usage for org.apache.commons.collections Transformer transform

List of usage examples for org.apache.commons.collections Transformer transform

Introduction

In this page you can find the example usage for org.apache.commons.collections Transformer transform.

Prototype

Object transform(Object input);

Source Link

Usage

From source file:com.phoenixst.plexus.traversals.PreOrderTraverser.java

/**
 *  Creates a new <code>PreOrderTraverser</code>.  If the
 *  <code>graph</code> argument is <code>null</code>, the
 *  specified <code>startNode</code> cannot be removed by {@link
 *  #remove}./*from   ww  w. jav a 2s.  co  m*/
 */
public PreOrderTraverser(Object startNode, Graph graph, Transformer traverserFactory) {
    super();
    this.traverserFactory = traverserFactory;

    if (traverserFactory == null) {
        throw new IllegalArgumentException("Traverser Factory is null.");
    }
    if (graph == null) {
        // This is the only way to make sure that startNode is in
        // the graph in this case.
        traverserFactory.transform(startNode);
    } else if (!graph.containsNode(startNode)) {
        throw new NoSuchNodeException("Graph does not contain start node: " + startNode);
    }

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Consturctor: Pushing trivial Traverser to " + startNode + " onto stack.");
    }
    traverserStack.push(new SingletonTraverser(graph, startNode, null));
}

From source file:com.phoenixst.plexus.traversals.BreadthFirstTraverser.java

/**
 *  Creates a new <code>BreadthFirstTraverser</code>.  If the
 *  <code>graph</code> argument is <code>null</code>, the
 *  specified <code>startNode</code> cannot be removed by {@link
 *  #remove}.//from   w  w  w  .  j  a  va  2  s. c  o  m
 */
public BreadthFirstTraverser(Object startNode, Graph graph, Transformer traverserFactory) {
    super();
    this.traverserFactory = traverserFactory;

    if (traverserFactory == null) {
        throw new IllegalArgumentException("Traverser Factory is null.");
    }
    if (graph == null) {
        // This is the only way to make sure that startNode is in
        // the graph in this case.
        traverserFactory.transform(startNode);
    } else if (!graph.containsNode(startNode)) {
        throw new NoSuchNodeException("Graph does not contain start node: " + startNode);
    }

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Constructor: Adding trivial Traverser to " + startNode + " to end of queue.");
    }
    traverserQueue.addLast(new SingletonTraverser(graph, startNode, null));
}

From source file:com.phoenixst.plexus.traversals.DepthFirstTraverser.java

/**
 *  Creates a new <code>DepthFirstTraverser</code>.  If the
 *  <code>graph</code> argument is <code>null</code>, the
 *  specified <code>startNode</code> cannot be removed by {@link
 *  #remove}./* w  w w.  j  av a 2s  .c o m*/
 */
public DepthFirstTraverser(Object startNode, Graph graph, Transformer traverserFactory) {
    super();
    this.traverserFactory = traverserFactory;

    if (traverserFactory == null) {
        throw new IllegalArgumentException("Traverser Factory is null.");
    }
    if (graph == null) {
        // This is the only way to make sure that startNode is in
        // the graph in this case.
        traverserFactory.transform(startNode);
    } else if (!graph.containsNode(startNode)) {
        throw new NoSuchNodeException("Graph does not contain start node: " + startNode);
    }

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Constructor: Pushing trivial Traverser to " + startNode + " onto Traverser stack.");
    }
    traverserStack.push(new SingletonTraverser(graph, startNode, null));
}

From source file:com.redhat.rhn.domain.monitoring.command.Command.java

/**
 * Check that values for a given metric are in monotonically increasing
 * order. For the metric <code>m</code>, look at the associated threshold
 * parameters and ensure that the values returned by <code>toValue</code>
 * for them are in strictly ascending order.
 * <p>/*  w  ww  .jav a 2  s. co  m*/
 * Each violation is entered into the returned list as four values: (param1,
 * value1, param2, value2) such that
 * <code>param1.thresholdType &lt; param2.thresholdType</code>, but
 * <code>value1 &gt;= value2</code> when compared as numbers. The
 * parameters in the returned list are of type {@link ThresholdParameter},
 * and the values are strings.
 *
 * @param metric the metric for which to check the parameter values
 * @param toValue a transformer mapping threshold parameters to their value
 * @return a list indicating which parameters have non-ascending values.
 */
public ArrayList checkAscendingValues(Metric metric, Transformer toValue) {
    ArrayList result = new ArrayList();
    ThresholdParameter prevParam = null;
    Float prevValue = null;
    String prevStr = null;
    for (Iterator j = listThresholds(metric).iterator(); j.hasNext();) {
        ThresholdParameter currParam = (ThresholdParameter) j.next();
        String currStr = (String) toValue.transform(currParam);
        Float currValue = null;
        try {
            currValue = NumberUtils.createFloat(currStr);
        } catch (NumberFormatException e) {
            // Ignore this value
            currValue = null;
        }
        if (currValue != null && prevValue != null) {
            if (currValue.compareTo(prevValue) <= 0) {
                result.add(prevParam);
                result.add(prevStr);
                result.add(currParam);
                result.add(currStr);
            }
        }
        if (currValue != null) {
            prevValue = currValue;
            prevParam = currParam;
            prevStr = currStr;
        }
    }
    assert result.size() % 4 == 0;
    return result;
}

From source file:com.ironiacorp.statistics.r.AbstractRClient.java

/**
 * @param matrix//from   ww  w . j a  v a2 s  . c o  m
 * @param matrixVarName
 * @param rowNameExtractor e.g. you could use StringValueTransformer.getInstance()
 * @return
 */
private void assignRowAndColumnNames(DoubleMatrix<?, ?> matrix, String matrixVarName,
        Transformer rowNameExtractor) {

    List<Object> rown = new ArrayList<Object>();
    for (Object o : matrix.getRowNames()) {
        rown.add(rowNameExtractor.transform(o));
    }

    String rowNameVar = assignStringList(rown);
    String colNameVar = assignStringList(matrix.getColNames());

    String dimcmd = "dimnames(" + matrixVarName + ")<-list(" + rowNameVar + ", " + colNameVar + ")";
    this.voidEval(dimcmd);
}

From source file:org.apache.cayenne.access.DataDomain.java

/**
 * Executes Transformer.transform() method in a transaction. Transaction policy is to
 * check for the thread transaction, and use it if one exists. If it doesn't, a new
 * transaction is created, with a scope limited to this method.
 *//* ww  w  . j  av a  2 s  . c  om*/
// WARNING: (andrus) if we ever decide to make this method protected or public, we
// need to change the signature to avoid API dependency on commons-collections
Object runInTransaction(Transformer operation) {

    // user or container-managed or nested transaction
    if (Transaction.getThreadTransaction() != null) {
        return operation.transform(null);
    }

    // Cayenne-managed transaction

    Transaction transaction = createTransaction();
    Transaction.bindThreadTransaction(transaction);

    try {
        // implicit begin..
        Object result = operation.transform(null);
        transaction.commit();
        return result;
    } catch (Exception ex) {
        transaction.setRollbackOnly();

        // must rethrow
        if (ex instanceof CayenneRuntimeException) {
            throw (CayenneRuntimeException) ex;
        } else {
            throw new CayenneRuntimeException(ex);
        }
    } finally {
        Transaction.bindThreadTransaction(null);
        if (transaction.getStatus() == Transaction.STATUS_MARKED_ROLLEDBACK) {
            try {
                transaction.rollback();
            } catch (Exception rollbackEx) {
                // although we don't expect an exception here, print the stack, as
                // there have been some Cayenne bugs already (CAY-557) that were
                // masked by this 'catch' clause.
                jdbcEventLogger.logQueryError(rollbackEx);
            }
        }
    }
}

From source file:org.apache.cayenne.access.SpringDataDomain.java

/**
 * Executes Transformer.transform() method in a transaction. Transaction
 * policy is to check for the thread transaction, and use it if one exists.
 * If it doesn't, a new transaction is created, with a scope limited to this
 * method.//from  w ww  . j a va  2 s  .  c om
 */
@Override
Object runInTransaction(final Transformer operation) {
    if (!this.isUsingExternalTransactions()) {
        return super.runInTransaction(operation);
    } else {
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            return operation.transform(null);
        }
        TransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
        return transationOperation.execute(transactionDefinition, new TransactionCallback<Object>() {

            @Override
            public Object doInTransaction(TransactionStatus status) {
                return operation.transform(status);
            }
        });
    }
}

From source file:org.apache.cayenne.exp.Expression.java

/**
 * A recursive method called from "transform" to do the actual
 * transformation.//from ww w  . j  av  a2 s  .  co  m
 * 
 * @return null, Expression.PRUNED_NODE or transformed expression.
 * @since 1.2
 */
protected Object transformExpression(Transformer transformer) {
    Expression copy = shallowCopy();
    int count = getOperandCount();
    for (int i = 0, j = 0; i < count; i++) {
        Object operand = getOperand(i);
        Object transformedChild;

        if (operand instanceof Expression) {
            transformedChild = ((Expression) operand).transformExpression(transformer);
        } else if (transformer != null) {
            transformedChild = transformer.transform(operand);
        } else {
            transformedChild = operand;
        }

        // prune null children only if there is a transformer and it
        // indicated so
        boolean prune = transformer != null && transformedChild == PRUNED_NODE;

        if (!prune) {
            copy.setOperand(j, transformedChild);
            j++;
        }

        if (prune && pruneNodeForPrunedChild(operand)) {
            // bail out early...
            return PRUNED_NODE;
        }
    }

    // all the children are processed, only now transform this copy
    return (transformer != null) ? (Expression) transformer.transform(copy) : copy;
}

From source file:org.candlepin.controller.PoolManagerFunctionalTest.java

@SuppressWarnings("unchecked")
public static <T> Set<T> transform(Set<?> set, Transformer t) {
    Set<T> result = Util.newSet();
    for (Iterator iterator = set.iterator(); iterator.hasNext();) {
        result.add((T) t.transform(iterator.next()));
    }// w  w w  .j a v  a2 s  .c  om
    return result;
}

From source file:org.examproject.tweet.service.SimpleCalendarService.java

/**
 * get the calendar dto list by name and year and month.
 *//*from w ww.j  a va2 s  .  com*/
@Override
public List<CalendarDto> getCalendarListByNameAndYearAndMonth(String username, Integer year, Integer month) {
    LOG.debug("called.");
    try {
        // create a date conditions.
        Transformer beginDateTransformer = (Transformer) new MonthBeginDateTransformer();
        Transformer endDateTransformer = (Transformer) new MonthEndDateTransformer();
        DateValue dateValue = new DateValue(year, month, 1);
        Date begin = (Date) beginDateTransformer.transform(dateValue);
        Date end = (Date) endDateTransformer.transform(dateValue);

        // get the tweet list.
        List<CalendarDto> calendarDtoList = new ArrayList<CalendarDto>();
        List<Tweet> tweetList = tweetRepository.findByNameAndDateBetween(username, begin, end);
        LOG.debug("calendar tweet day count: " + tweetList.size());

        // create values for the month. 
        for (int i = 0; i < 31; i++) {
            CalendarDto calendarDto = context.getBean(CalendarDto.class);
            calendarDto.setDay(i + 1);
            calendarDto.setCount(0);
            calendarDtoList.add(calendarDto);
        }

        // check the existence of tweet.
        SimpleDateFormat dateFormat = new SimpleDateFormat("d");
        SimpleDateFormat linkDateFormat = new SimpleDateFormat("/yyyy/MM/dd");
        for (Tweet tweet : tweetList) {
            int dIdx = Integer.parseInt(dateFormat.format(tweet.getDate()));
            CalendarDto calendarDto = calendarDtoList.get(dIdx - 1);
            calendarDto.setDate(tweet.getDate());
            calendarDto.setLinkUrl("/tweet/" + username + linkDateFormat.format(tweet.getDate()) + ".html");
            calendarDto.setIsExist(true);
            calendarDto.setCount(calendarDto.getCount() + 1);
        }

        // return the object list.
        return calendarDtoList;

    } catch (Exception e) {
        LOG.error("an error occurred: " + e.getMessage());
        throw new RuntimeException(e);
    }
}