List of usage examples for java.util LinkedList getLast
public E getLast()
From source file:org.eclipse.swordfish.core.configuration.xml.SaxParsingPrototype.java
/** * @param args/*from www.j a va 2 s.co m*/ */ public static void main(String[] args) throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); LinkedList<String> currentElements = new LinkedList<String>(); InputStream in = SaxParsingPrototype.class.getResource("ComplexPidXmlProperties.xml").openStream(); XMLEventReader eventReader = inputFactory.createXMLEventReader(in); Map<String, List<String>> props = new HashMap<String, List<String>>(); // Read the XML document while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) { putElement(props, getQualifiedName(currentElements), event.asCharacters().getData()); } else if (event.isStartElement()) { System.out.println(event.asStartElement().getName()); currentElements.add(event.asStartElement().getName().getLocalPart()); for (Iterator attrIt = event.asStartElement().getAttributes(); attrIt.hasNext();) { Attribute attribute = (Attribute) attrIt.next(); putElement(props, getQualifiedName(currentElements) + "[@" + attribute.getName() + "]", attribute.getValue()); } } else if (event.isAttribute()) { } else if (event.isEndElement()) { String lastElem = event.asEndElement().getName().getLocalPart(); if (!currentElements.getLast().equals(lastElem)) { throw new UnsupportedOperationException(lastElem + "," + currentElements.getLast()); } currentElements.removeLast(); } } eventReader.close(); }
From source file:Main.java
/** * Returns the last element of list. If the given list is empty, the * returned value will be null.// w w w. j av a2 s . c o m * * @param <T> Class type * @param collection List of elements * @return Last element of list */ public static <T> T getLastElement(Collection<T> collection) { LinkedList<T> elements = collection == null ? new LinkedList<>() : new LinkedList<>(collection); return elements.isEmpty() ? null : elements.getLast(); }
From source file:Main.java
/** * Generate a hash chain of a random number * @param r1/* ww w . j a va2s.c o m*/ * @param numHash * @return * @throws NoSuchAlgorithmException * @throws UnsupportedEncodingException */ public static LinkedList<BigInteger> getHashChain(BigInteger r1, int numHash) throws NoSuchAlgorithmException, UnsupportedEncodingException { LinkedList<BigInteger> hashChain = new LinkedList<BigInteger>(); hashChain.add(r1); for (int i = 1; i < numHash; i++) { byte[] lastR = hashChain.getLast().toByteArray(); hashChain.add(new BigInteger(1, SHA1(lastR))); } return hashChain; }
From source file:Main.java
/** * Normalize a uri containing ../ and ./ paths. * * @param uri The uri path to normalize//from w w w . j a v a 2 s. c om * @return The normalized uri */ public static String normalize(String uri) { if ("".equals(uri)) { return uri; } int leadingSlashes; for (leadingSlashes = 0; leadingSlashes < uri.length() && uri.charAt(leadingSlashes) == '/'; ++leadingSlashes) { } boolean isDir = (uri.charAt(uri.length() - 1) == '/'); StringTokenizer st = new StringTokenizer(uri, "/"); LinkedList clean = new LinkedList(); while (st.hasMoreTokens()) { String token = st.nextToken(); if ("..".equals(token)) { if (!clean.isEmpty() && !"..".equals(clean.getLast())) { clean.removeLast(); if (!st.hasMoreTokens()) { isDir = true; } } else { clean.add(".."); } } else if (!".".equals(token) && !"".equals(token)) { clean.add(token); } } StringBuffer sb = new StringBuffer(); while (leadingSlashes-- > 0) { sb.append('/'); } for (Iterator it = clean.iterator(); it.hasNext();) { sb.append(it.next()); if (it.hasNext()) { sb.append('/'); } } if (isDir && sb.length() > 0 && sb.charAt(sb.length() - 1) != '/') { sb.append('/'); } return sb.toString(); }
From source file:com.cyclopsgroup.waterview.utils.TagSupportBase.java
/** * Remove last script resource// w w w .j a v a 2 s . c o m * * @param resource Resource to remove * @param context Jelly context */ public static final void removeScriptResource(URL resource, JellyContext context) { synchronized (context) { LinkedList scriptResources = (LinkedList) context.getVariable(SCRIPT_RESOURCE_NAME); if (scriptResources == null) { return; } URL last = (URL) scriptResources.getLast(); if (last.sameFile(resource)) { scriptResources.removeLast(); } else { throw new IllegalStateException("The resource to remove is not the last resource"); } } }
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 w w w. j av a 2s . c om 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:org.apache.cocoon.util.NetUtils.java
/** * Normalize a uri containing ../ and ./ paths. * * @param uri The uri path to normalize/*from ww w. ja va 2 s. co m*/ * @return The normalized uri */ public static String normalize(String uri) { if ("".equals(uri)) { return uri; } int leadingSlashes = 0; for (leadingSlashes = 0; leadingSlashes < uri.length() && uri.charAt(leadingSlashes) == '/'; ++leadingSlashes) { } boolean isDir = (uri.charAt(uri.length() - 1) == '/'); StringTokenizer st = new StringTokenizer(uri, "/"); LinkedList clean = new LinkedList(); while (st.hasMoreTokens()) { String token = st.nextToken(); if ("..".equals(token)) { if (!clean.isEmpty() && !"..".equals(clean.getLast())) { clean.removeLast(); if (!st.hasMoreTokens()) { isDir = true; } } else { clean.add(".."); } } else if (!".".equals(token) && !"".equals(token)) { clean.add(token); } } StringBuffer sb = new StringBuffer(); while (leadingSlashes-- > 0) { sb.append('/'); } for (Iterator it = clean.iterator(); it.hasNext();) { sb.append(it.next()); if (it.hasNext()) { sb.append('/'); } } if (isDir && sb.length() > 0 && sb.charAt(sb.length() - 1) != '/') { sb.append('/'); } return sb.toString(); }
From source file:jp.co.ctc_g.jse.vid.ViewId.java
private static ViewId current(ViewIdStore store) { ViewId id = null;//from w w w . ja v a 2 s . co m synchronized (store.semaphore()) { LinkedList<ViewId> ids = store.find(false); id = ids != null && !ids.isEmpty() ? ids.getLast() : null; } return id; }
From source file:org.apache.hadoop.hdfs.server.namenode.AbstractFileTree.java
/** * This method is for testing only! Do not rely on it. * * @param path//from ww w . j a v a2s .c o m * The path of the subtree * @return A FileTree instance reprecenting the path * @throws io.hops.exception.StorageException * @throws org.apache.hadoop.hdfs.protocol.UnresolvedPathException */ @VisibleForTesting static CountingFileTree createCountingFileTreeFromPath(FSNamesystem namesystem, String path) throws StorageException, UnresolvedPathException, TransactionContextException, AccessControlException { LinkedList<INode> nodes = new LinkedList<>(); INodeUtil.resolvePathWithNoTransaction(path, false, nodes); INodeIdentifier rootId = new INodeIdentifier(nodes.getLast().getId(), nodes.getLast().getParentId(), nodes.getLast().getLocalName(), nodes.getLast().getPartitionId()); rootId.setDepth((short) (INodeDirectory.ROOT_DIR_DEPTH + (nodes.size() - 1))); return new CountingFileTree(namesystem, rootId, BlockStoragePolicySuite.ID_UNSPECIFIED); }
From source file:org.apache.hadoop.hdfs.server.namenode.AbstractFileTree.java
/** * This method id for testing only! Do not rely on it. * * @param path// w ww. j a v a 2 s.c om * The path of the subtree * @return A FileTree instance reprecenting the path * @throws io.hops.exception.StorageException * @throws UnresolvedPathException */ @VisibleForTesting static FileTree createFileTreeFromPath(FSNamesystem namesystem, String path) throws StorageException, UnresolvedPathException, TransactionContextException, AccessControlException { LinkedList<INode> nodes = new LinkedList<>(); INodeUtil.resolvePathWithNoTransaction(path, false, nodes); INodeIdentifier rootId = new INodeIdentifier(nodes.getLast().getId(), nodes.getLast().getParentId(), nodes.getLast().getLocalName(), nodes.getLast().getPartitionId()); rootId.setDepth((short) (INodeDirectory.ROOT_DIR_DEPTH + (nodes.size() - 1))); return new FileTree(namesystem, rootId); }