List of usage examples for java.util List clear
void clear();
From source file:hr.fer.tel.rovkp.homework03.task01.JokesCollection.java
public static Map<Integer, String> parseInputFile(String file) throws IOException { Path filePath = Paths.get(file); Map<Integer, String> results = new HashMap<>(); List<String> currentJoke = new LinkedList<>(); LinkedList<Integer> ids = new LinkedList<>(); try (Stream<String> stream = Files.lines(filePath)) { stream.forEach(line -> {//from ww w . j a v a 2s .c o m if (line == null) return; line = line.trim(); if (line.isEmpty() && !currentJoke.isEmpty()) { int currentId = ids.getLast(); String jokeText = StringUtils.join(currentJoke, "\n"); jokeText = StringEscapeUtils.unescapeXml(jokeText.toLowerCase().replaceAll("\\<.*?\\>", "")); if (results.putIfAbsent(currentId, jokeText) != null) System.err.println("Joke with id " + currentId + "already exists. Not overwriting."); } else if (line.matches("^[0-9]+:$")) { ids.addLast(Integer.parseInt(line.substring(0, line.length() - 1))); currentJoke.clear(); } else { currentJoke.add(line); } }); } return results; }
From source file:com.orange.ocara.model.export.docx.AuditObjectPresenter.java
private void buildComments(List<CommentPresenter> commentPresenters, Comment.Type type) { commentPresenters.clear(); for (Comment comment : value.getComments()) { if (type.equals(comment.getType())) { commentPresenters.add(new CommentPresenter(comment)); }/*www . j a va 2s .c om*/ } }
From source file:com.yolodata.tbana.hadoop.mapred.util.CSVReader.java
public int readLine(List<Text> values) throws IOException { values.clear(); StringBuffer line = new StringBuffer(); int charactersRead = 0; char currentChar; boolean insideQuotes = false; int i, quoteOffset = 0, delimiterOffset = 0; while ((i = reader.read()) != -1) { currentChar = (char) i; charactersRead++;// ww w . j a va 2 s . co m line.append(currentChar); // Check quotes, as delimiter inside quotes don't count if (currentChar == delimiter.charAt(quoteOffset)) { quoteOffset++; if (quoteOffset >= delimiter.length()) { insideQuotes = !insideQuotes; quoteOffset = 0; } } else { quoteOffset = 0; } // Check delimiters, but only those outside of quotes if (!insideQuotes) { if (currentChar == separator.charAt(delimiterOffset)) { delimiterOffset++; if (delimiterOffset >= separator.length()) { foundDelimiter(line, values, true); delimiterOffset = 0; } } else { delimiterOffset = 0; } // A new line outside of a quote is a real csv line breaker if (currentChar == '\n') { break; } } } foundDelimiter(line, values, false); return charactersRead; }
From source file:de.fhg.igd.mapviewer.server.file.FileTiler.java
/** * Tries to exec the command, waits for it to finsih, logs errors if exit * status is nonzero, and returns true if exit status is 0 (success). * * @param command Description of the Parameter * @param output a list that will be cleared and the output lines added (if * the list is not null)/*from w w w.j a v a2s.c om*/ * @return Description of the Return Value */ public static boolean exec(String[] command, List<String> output) { Process proc; try { // System.out.println("Trying to execute command " + // Arrays.asList(command)); proc = Runtime.getRuntime().exec(command); } catch (IOException e) { log.error("IOException while trying to execute " + Arrays.toString(command), e); return false; } if (output == null) output = new ArrayList<String>(); output.clear(); BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream())); String currentLine; try { while ((currentLine = reader.readLine()) != null) { output.add(currentLine); } } catch (IOException e) { log.error("Error reading process output", e); } finally { try { reader.close(); } catch (IOException e) { log.error("Error closing input stream", e); } } int exitStatus; while (true) { try { exitStatus = proc.waitFor(); break; } catch (java.lang.InterruptedException e) { log.warn("Interrupted: Ignoring and waiting"); } } if (exitStatus != 0) { /* * StringBuilder cmdString = new StringBuilder(); for (int i = 0; i * < command.length; i++) cmdString.append(command[i]); */ log.warn("Error executing command: " + exitStatus + " (" + output + ")"); } return (exitStatus == 0); }
From source file:com.redhat.persistence.oql.MultiMap.java
private List allocateValues() { if (m_free.isEmpty()) { return SetUniqueList.decorate(new ArrayList()); } else {/* w w w. j av a2 s.c o m*/ List result = (List) m_free.remove(m_free.size() - 1); result.clear(); return result; } }
From source file:io.scigraph.services.jersey.writers.DelimitedWriter.java
@Override public void writeTo(Graph data, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> headers, OutputStream out) throws IOException { try (Writer writer = new OutputStreamWriter(out); CSVPrinter printer = getCsvPrinter(writer)) { List<String> header = newArrayList("id", "label", "categories"); printer.printRecord(header);//from w ww . j av a 2s .co m List<String> vals = new ArrayList<>(); for (Vertex vertex : data.getVertices()) { vals.clear(); vals.add(getCurieOrIri(vertex)); String label = getFirst(TinkerGraphUtil.getProperties(vertex, NodeProperties.LABEL, String.class), null); vals.add(label); vals.add(TinkerGraphUtil.getProperties(vertex, Concept.CATEGORY, String.class).toString()); printer.printRecord(vals); } } }
From source file:com.mirth.connect.server.util.DICOMMessageUtil.java
public static byte[] mergeHeaderPixelData(byte[] header, List<Attachment> attachments) throws IOException { // 1. read in header DicomObject dcmObj = DICOMConverter.byteArrayToDicomObject(header, false); // 2. Add pixel data to DicomObject if (attachments != null && !attachments.isEmpty()) { if (attachments.size() > 1) { DicomElement dicomElement = dcmObj.putFragments(Tag.PixelData, VR.OB, dcmObj.bigEndian(), attachments.size()); for (Attachment attachment : attachments) { dicomElement.addFragment(attachment.getContent()); }//from w w w . j a v a 2 s.co m dcmObj.add(dicomElement); } else { dcmObj.putBytes(Tag.PixelData, VR.OB, attachments.get(0).getContent()); } } // Memory Optimization. Free the references to the data in the attachments list. attachments.clear(); return DICOMConverter.dicomObjectToByteArray(dcmObj); }
From source file:com.astamuse.asta4d.render.RenderUtil.java
private final static void apply(Element target, List<Renderer> rendererList, RenderAction renderAction, int startIndex, int count) { // The renderer list have to be applied recursively because the // transformer will always return a new Element clone. if (startIndex >= count) { return;/*ww w .j a v a2 s .com*/ } final Renderer currentRenderer = rendererList.get(startIndex); RendererType rendererType = currentRenderer.getRendererType(); switch (rendererType) { case GO_THROUGH: apply(target, rendererList, renderAction, startIndex + 1, count); return; /* case DEBUG: currentRenderer.getTransformerList().get(0).invoke(target); apply(target, rendererList, renderAction, startIndex + 1, count); return; */ case RENDER_ACTION: ((RenderActionRenderer) currentRenderer).getStyle().apply(renderAction); apply(target, rendererList, renderAction, startIndex + 1, count); return; default: // do nothing break; } String selector = currentRenderer.getSelector(); List<Transformer<?>> transformerList = currentRenderer.getTransformerList(); List<Element> elemList; if (PSEUDO_ROOT_SELECTOR.equals(selector)) { elemList = new LinkedList<Element>(); elemList.add(target); } else { elemList = new ArrayList<>(target.select(selector)); } if (elemList.isEmpty()) { if (rendererType == RendererType.ELEMENT_NOT_FOUND_HANDLER) { elemList.add(target); transformerList.clear(); transformerList.add( new RendererTransformer(((ElementNotFoundHandler) currentRenderer).alternativeRenderer())); } else if (renderAction.isOutputMissingSelectorWarning()) { String creationInfo = currentRenderer.getCreationSiteInfo(); if (creationInfo == null) { creationInfo = ""; } else { creationInfo = " at [ " + creationInfo + " ]"; } logger.warn( "There is no element found for selector [{}]{}, if it is deserved, try Renderer#disableMissingSelectorWarning() " + "to disable this message and Renderer#enableMissingSelectorWarning could enable this warning again in " + "your renderer chain", selector, creationInfo); apply(target, rendererList, renderAction, startIndex + 1, count); return; } } else { if (rendererType == RendererType.ELEMENT_NOT_FOUND_HANDLER) { apply(target, rendererList, renderAction, startIndex + 1, count); return; } } Element delayedElement = null; Element resultNode; // TODO we suppose that the element is listed as the order from parent // to children, so we reverse it. Perhaps we need a real order process // to ensure the wanted order. Collections.reverse(elemList); boolean renderForRoot; for (Element elem : elemList) { renderForRoot = PSEUDO_ROOT_SELECTOR.equals(selector) || rendererType == RendererType.ELEMENT_NOT_FOUND_HANDLER; if (!renderForRoot) { // faked group node will be not applied by renderers(only when the current selector is not the pseudo :root) if (elem.tagName().equals(ExtNodeConstants.GROUP_NODE_TAG) && ExtNodeConstants.GROUP_NODE_ATTR_TYPE_FAKE .equals(elem.attr(ExtNodeConstants.GROUP_NODE_ATTR_TYPE))) { continue; } } if (elem == target) { delayedElement = elem; continue; } for (Transformer<?> transformer : transformerList) { resultNode = transformer.invoke(elem); elem.before(resultNode); } // for transformer elem.remove(); } // for element // if the root element is one of the process targets, we can not apply // the left renderers to original element because it will be replaced by // a new element even it is not necessary (that is how Transformer // works). if (delayedElement == null) { apply(target, rendererList, renderAction, startIndex + 1, count); } else { if (rendererType == RendererType.ELEMENT_NOT_FOUND_HANDLER && delayedElement instanceof Document) { delayedElement = delayedElement.child(0); } for (Transformer<?> transformer : transformerList) { resultNode = transformer.invoke(delayedElement); delayedElement.before(resultNode); apply(resultNode, rendererList, renderAction, startIndex + 1, count); } // for transformer delayedElement.remove(); } }
From source file:edu.uci.ics.hyracks.algebricks.rewriter.rules.BreakSelectIntoConjunctsRule.java
@Override public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException { AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue(); if (op.getOperatorTag() != LogicalOperatorTag.SELECT) { return false; }//from w ww . ja v a 2s .com SelectOperator select = (SelectOperator) op; ILogicalExpression cond = select.getCondition().getValue(); conjs.clear(); if (!cond.splitIntoConjuncts(conjs)) { return false; } Mutable<ILogicalOperator> childOfSelect = select.getInputs().get(0); boolean fst = true; ILogicalOperator botOp = select; ILogicalExpression firstExpr = null; for (Mutable<ILogicalExpression> eRef : conjs) { ILogicalExpression e = eRef.getValue(); if (fst) { fst = false; firstExpr = e; } else { SelectOperator newSelect = new SelectOperator(new MutableObject<ILogicalExpression>(e), select.getRetainNull(), select.getNullPlaceholderVariable()); List<Mutable<ILogicalOperator>> botInpList = botOp.getInputs(); botInpList.clear(); botInpList.add(new MutableObject<ILogicalOperator>(newSelect)); context.computeAndSetTypeEnvironmentForOperator(botOp); botOp = newSelect; } } botOp.getInputs().add(childOfSelect); select.getCondition().setValue(firstExpr); context.computeAndSetTypeEnvironmentForOperator(botOp); context.computeAndSetTypeEnvironmentForOperator(select); return true; }
From source file:com.meltmedia.cadmium.servlets.FileServletTest.java
public static HttpServletResponse mockResponse(final Map<String, List<String>> headers, OutputStream output) throws IOException { HttpServletResponse response = mock(HttpServletResponse.class); when(response.getOutputStream()).thenReturn(new MockServletOutputStream(output)); if (headers != null) { Answer<Void> addHeaderAnswer = new Answer<Void>() { @Override//ww w . j a v a 2 s. c o m public Void answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); List<String> value = headers.get((String) args[0]); if (value == null) headers.put((String) args[0], value = new ArrayList<String>()); value.add(args[1].toString()); return null; } }; Answer<Void> setHeaderAnswer = new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); List<String> value = headers.get((String) args[0]); if (value == null) headers.put((String) args[0], value = new ArrayList<String>()); value.clear(); value.add(args[1].toString()); return null; } }; doAnswer(setHeaderAnswer).when(response).setHeader(anyString(), anyString()); doAnswer(setHeaderAnswer).when(response).setDateHeader(anyString(), anyLong()); doAnswer(setHeaderAnswer).when(response).setIntHeader(anyString(), anyInt()); doAnswer(addHeaderAnswer).when(response).addHeader(anyString(), anyString()); doAnswer(addHeaderAnswer).when(response).addDateHeader(anyString(), anyLong()); doAnswer(addHeaderAnswer).when(response).addIntHeader(anyString(), anyInt()); } return response; }