下面列出了java.util.LinkedList#getLast ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public LinkedList<BorderPartitionInfo> getPartitionsList() {
LinkedList<BorderPartitionInfo> resultList = new LinkedList<>();
resultList.addAll(partitionsList);
if (resultList.size() >= 2 && resultList.getFirst().partitionId == resultList.getLast().partitionId && lastPartition != -1) {
resultList.removeFirst();
}
return resultList;
}
public T getJAXBObject(MCRContent source, MCRParameterCollector parameter)
throws TransformerConfigurationException, SAXException, JAXBException, IOException,
ParserConfigurationException {
LinkedList<TransformerHandler> transformHandlerList = getTransformHandlerList(parameter);
XMLReader reader = getXMLReader(transformHandlerList);
TransformerHandler lastTransformerHandler = transformHandlerList.getLast();
return getJAXBObject(source, reader, lastTransformerHandler);
}
private void addDatum(String name, double value, LinkedList<PutMetricDataRequest> requests, Date timestamp) {
if (logger.isDebugEnabled()) {
logger.debug("Adding Datum {} with value {} at {}", name, value, timestamp);
}
if (requests.isEmpty() || requests.getLast().getMetricData().size() == MAX_CLOUDWATCH_DATUM_PER_REQUEST) {
requests.add(createRequest());
}
PutMetricDataRequest request = requests.getLast();
MetricDatum datum = new MetricDatum().withTimestamp(timestamp).withValue(value).withMetricName(name).withUnit(StandardUnit.None).withDimensions(createDimensions());
request.withMetricData(datum);
}
@Override
public MCRContent transform(MCRContent source, MCRParameterCollector parameter) throws IOException {
try {
LinkedList<TransformerHandler> transformHandlerList = getTransformHandlerList(parameter);
XMLReader reader = getXMLReader(transformHandlerList);
TransformerHandler lastTransformerHandler = transformHandlerList.getLast();
return transform(source, reader, lastTransformerHandler, parameter);
} catch (TransformerException | SAXException | ParserConfigurationException e) {
throw new IOException(e);
}
}
/**
* getLast returns next element, or throws NSEE if empty
*/
public void testLastElement() {
LinkedList q = populatedQueue(SIZE);
for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.getLast());
assertEquals(i, q.pollLast());
}
try {
q.getLast();
shouldThrow();
} catch (NoSuchElementException success) {}
assertNull(q.peekLast());
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String s1 = req.getParameter(FIELD_NAME);
LinkedList ll = new LinkedList();
ll.addLast(s1);
String s2 = (String) ll.getLast();
PrintWriter writer = resp.getWriter();
writer.println(s2); /* BAD */
}
/**
* The start time of the first parameter within the first time slice has to
* be extended to the beginning of the time slice.
*
* @param timeSlice
* the first time slice having parameters of the billing relevant
* period
* @param valuesPerSlice
* all parameter values for the given slice, the list will be
* modified by this method
*/
private void updatePeriodFactorFirstSliceFirstValue(TimeSlice timeSlice,
LinkedList<XParameterPeriodValue> valuesPerSlice) {
XParameterPeriodValue firstPeriodValue = valuesPerSlice.getLast();
updateParameterPeriodFactor(
timeSlice,
firstPeriodValue,
timeSlice.getStartAsCalendar(),
parameterEndTimeForPeriodCalculation(timeSlice,
firstPeriodValue));
valuesPerSlice.removeLast();
}
/**
* Call the {@link LineHandler}
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
try (Closeable closeable = mdcContextFactory.from(protocol, ctx)) {
ProtocolSession pSession = (ProtocolSession) ctx.getAttachment();
LinkedList<LineHandler> lineHandlers = chain.getHandlers(LineHandler.class);
LinkedList<ProtocolHandlerResultHandler> resultHandlers = chain.getHandlers(ProtocolHandlerResultHandler.class);
if (lineHandlers.size() > 0) {
ChannelBuffer buf = (ChannelBuffer) e.getMessage();
LineHandler lHandler = (LineHandler) lineHandlers.getLast();
long start = System.currentTimeMillis();
Response response = lHandler.onLine(pSession, buf.toByteBuffer());
long executionTime = System.currentTimeMillis() - start;
for (ProtocolHandlerResultHandler resultHandler : resultHandlers) {
response = resultHandler.onResponse(pSession, response, executionTime, lHandler);
}
if (response != null) {
// TODO: This kind of sucks but I was able to come up with something more elegant here
((ProtocolSessionImpl) pSession).getProtocolTransport().writeResponse(response, pSession);
}
}
super.messageReceived(ctx, e);
}
}
private void validateIfBlockIsConnecting(RawBlock rawBlock) throws BlockNotConnectingException {
LinkedList<Block> blocks = bsqStateService.getBlocks();
if (!isBlockConnecting(rawBlock, blocks)) {
final Block last = blocks.getLast();
log.warn("addBlock called with a not connecting block. New block:\n" +
"height()={}, hash()={}, lastBlock.height()={}, lastBlock.hash()={}",
rawBlock.getHeight(),
rawBlock.getHash(),
last != null ? last.getHeight() : "null",
last != null ? last.getHash() : "null");
throw new BlockNotConnectingException(rawBlock);
}
}
public Invocation findPreviousVerifiedInOrder(List<Invocation> invocations, InOrderContext context) {
LinkedList<Invocation> verifiedOnly = ListUtil.filter(invocations, new RemoveUnverifiedInOrder(context));
if (verifiedOnly.isEmpty()) {
return null;
} else {
return verifiedOnly.getLast();
}
}
/**
* Compute a list of patches to turn text1 into text2.
* text2 is not provided, diffs are the delta between text1 and text2.
* @param text1 Old text.
* @param diffs Array of Diff objects for text1 to text2.
* @return LinkedList of Patch objects.
*/
public LinkedList<Patch> patch_make(String text1, LinkedList<Diff> diffs) {
if (text1 == null || diffs == null) {
throw new IllegalArgumentException("Null inputs. (patch_make)");
}
LinkedList<Patch> patches = new LinkedList<Patch>();
if (diffs.isEmpty()) {
return patches; // Get rid of the null case.
}
Patch patch = new Patch();
int char_count1 = 0; // Number of characters into the text1 string.
int char_count2 = 0; // Number of characters into the text2 string.
// Start with text1 (prepatch_text) and apply the diffs until we arrive at
// text2 (postpatch_text). We recreate the patches one by one to determine
// context info.
String prepatch_text = text1;
String postpatch_text = text1;
for (Diff aDiff : diffs) {
if (patch.diffs.isEmpty() && aDiff.operation != Operation.EQUAL) {
// A new patch starts here.
patch.start1 = char_count1;
patch.start2 = char_count2;
}
switch (aDiff.operation) {
case INSERT:
patch.diffs.add(aDiff);
patch.length2 += aDiff.text.length();
postpatch_text = postpatch_text.substring(0, char_count2)
+ aDiff.text + postpatch_text.substring(char_count2);
break;
case DELETE:
patch.length1 += aDiff.text.length();
patch.diffs.add(aDiff);
postpatch_text = postpatch_text.substring(0, char_count2)
+ postpatch_text.substring(char_count2 + aDiff.text.length());
break;
case EQUAL:
if (aDiff.text.length() <= 2 * Patch_Margin
&& !patch.diffs.isEmpty() && aDiff != diffs.getLast()) {
// Small equality inside a patch.
patch.diffs.add(aDiff);
patch.length1 += aDiff.text.length();
patch.length2 += aDiff.text.length();
}
if (aDiff.text.length() >= 2 * Patch_Margin && !patch.diffs.isEmpty()) {
// Time for a new patch.
if (!patch.diffs.isEmpty()) {
patch_addContext(patch, prepatch_text);
patches.add(patch);
patch = new Patch();
// Unlike Unidiff, our patch lists have a rolling context.
// https://github.com/google/diff-match-patch/wiki/Unidiff
// Update prepatch text & pos to reflect the application of the
// just completed patch.
prepatch_text = postpatch_text;
char_count1 = char_count2;
}
}
break;
}
// Update the current character count.
if (aDiff.operation != Operation.INSERT) {
char_count1 += aDiff.text.length();
}
if (aDiff.operation != Operation.DELETE) {
char_count2 += aDiff.text.length();
}
}
// Pick up the leftover patch if not empty.
if (!patch.diffs.isEmpty()) {
patch_addContext(patch, prepatch_text);
patches.add(patch);
}
return patches;
}
/**
* Compute a list of patches to turn text1 into text2.
* text2 is not provided, diffs are the delta between text1 and text2.
*
* @param text1 Old text.
* @param diffs Array of Diff objects for text1 to text2.
* @return LinkedList of Patch objects.
*/
public LinkedList<Patch> patch_make(String text1, LinkedList<Diff> diffs) {
if (text1 == null || diffs == null) {
throw new IllegalArgumentException("Null inputs. (patch_make)");
}
LinkedList<Patch> patches = new LinkedList<Patch>();
if (diffs.isEmpty()) {
return patches; // Get rid of the null case.
}
Patch patch = new Patch();
int char_count1 = 0; // Number of characters into the text1 string.
int char_count2 = 0; // Number of characters into the text2 string.
// Start with text1 (prepatch_text) and apply the diffs until we arrive at
// text2 (postpatch_text). We recreate the patches one by one to determine
// context info.
String prepatch_text = text1;
String postpatch_text = text1;
for (Diff aDiff : diffs) {
if (patch.diffs.isEmpty() && aDiff.operation != Operation.EQUAL) {
// A new patch starts here.
patch.start1 = char_count1;
patch.start2 = char_count2;
}
switch (aDiff.operation) {
case INSERT:
patch.diffs.add(aDiff);
patch.length2 += aDiff.text.length();
postpatch_text = postpatch_text.substring(0, char_count2)
+ aDiff.text + postpatch_text.substring(char_count2);
break;
case DELETE:
patch.length1 += aDiff.text.length();
patch.diffs.add(aDiff);
postpatch_text = postpatch_text.substring(0, char_count2)
+ postpatch_text.substring(char_count2 + aDiff.text.length());
break;
case EQUAL:
if (aDiff.text.length() <= 2 * Patch_Margin
&& !patch.diffs.isEmpty() && aDiff != diffs.getLast()) {
// Small equality inside a patch.
patch.diffs.add(aDiff);
patch.length1 += aDiff.text.length();
patch.length2 += aDiff.text.length();
}
if (aDiff.text.length() >= 2 * Patch_Margin && !patch.diffs.isEmpty()) {
// Time for a new patch.
if (!patch.diffs.isEmpty()) {
patch_addContext(patch, prepatch_text);
patches.add(patch);
patch = new Patch();
// Unlike Unidiff, our patch lists have a rolling context.
// https://github.com/google/diff-match-patch/wiki/Unidiff
// Update prepatch text & pos to reflect the application of the
// just completed patch.
prepatch_text = postpatch_text;
char_count1 = char_count2;
}
}
break;
}
// Update the current character count.
if (aDiff.operation != Operation.INSERT) {
char_count1 += aDiff.text.length();
}
if (aDiff.operation != Operation.DELETE) {
char_count2 += aDiff.text.length();
}
}
// Pick up the leftover patch if not empty.
if (!patch.diffs.isEmpty()) {
patch_addContext(patch, prepatch_text);
patches.add(patch);
}
return patches;
}
/**
* Compute a list of patches to turn text1 into text2.
* text2 is not provided, diffs are the delta between text1 and text2.
* @param text1 Old text.
* @param diffs Array of Diff objects for text1 to text2.
* @return LinkedList of Patch objects.
*/
public LinkedList<Patch> patch_make(String text1, LinkedList<Diff> diffs) {
if (text1 == null || diffs == null) {
throw new IllegalArgumentException("Null inputs. (patch_make)");
}
LinkedList<Patch> patches = new LinkedList<Patch>();
if (diffs.isEmpty()) {
return patches; // Get rid of the null case.
}
Patch patch = new Patch();
int char_count1 = 0; // Number of characters into the text1 string.
int char_count2 = 0; // Number of characters into the text2 string.
// Start with text1 (prepatch_text) and apply the diffs until we arrive at
// text2 (postpatch_text). We recreate the patches one by one to determine
// context info.
String prepatch_text = text1;
String postpatch_text = text1;
for (Diff aDiff : diffs) {
if (patch.diffs.isEmpty() && aDiff.operation != Operation.EQUAL) {
// A new patch starts here.
patch.start1 = char_count1;
patch.start2 = char_count2;
}
switch (aDiff.operation) {
case INSERT:
patch.diffs.add(aDiff);
patch.length2 += aDiff.text.length();
postpatch_text = postpatch_text.substring(0, char_count2)
+ aDiff.text + postpatch_text.substring(char_count2);
break;
case DELETE:
patch.length1 += aDiff.text.length();
patch.diffs.add(aDiff);
postpatch_text = postpatch_text.substring(0, char_count2)
+ postpatch_text.substring(char_count2 + aDiff.text.length());
break;
case EQUAL:
if (aDiff.text.length() <= 2 * Patch_Margin
&& !patch.diffs.isEmpty() && aDiff != diffs.getLast()) {
// Small equality inside a patch.
patch.diffs.add(aDiff);
patch.length1 += aDiff.text.length();
patch.length2 += aDiff.text.length();
}
if (aDiff.text.length() >= 2 * Patch_Margin) {
// Time for a new patch.
if (!patch.diffs.isEmpty()) {
patch_addContext(patch, prepatch_text);
patches.add(patch);
patch = new Patch();
// Unlike Unidiff, our patch lists have a rolling context.
// http://code.google.com/p/google-diff-match-patch/wiki/Unidiff
// Update prepatch text & pos to reflect the application of the
// just completed patch.
prepatch_text = postpatch_text;
char_count1 = char_count2;
}
}
break;
}
// Update the current character count.
if (aDiff.operation != Operation.INSERT) {
char_count1 += aDiff.text.length();
}
if (aDiff.operation != Operation.DELETE) {
char_count2 += aDiff.text.length();
}
}
// Pick up the leftover patch if not empty.
if (!patch.diffs.isEmpty()) {
patch_addContext(patch, prepatch_text);
patches.add(patch);
}
return patches;
}
/**
* Set the port of the (shared) default endpoint object.
* When first created, it contains port 0 because the transport
* hasn't tried to listen to get assigned a port, or if listening
* failed, a port hasn't been assigned from the server.
*/
static void setDefaultPort(int port, RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
{
TCPEndpoint endpointKey = new TCPEndpoint(null, 0, csf, ssf);
synchronized (localEndpoints) {
LinkedList<TCPEndpoint> epList = localEndpoints.get(endpointKey);
synchronized (epList) {
int size = epList.size();
TCPEndpoint lastEp = epList.getLast();
for (TCPEndpoint ep : epList) {
ep.port = port;
}
if (size > 1) {
/*
* Remove all but the last element of the list
* (which contains the most recent hostname).
*/
epList.clear();
epList.add(lastEp);
}
}
/*
* Allow future exports to use the actual bound port
* explicitly (see 6269166).
*/
TCPEndpoint newEndpointKey = new TCPEndpoint(null, port, csf, ssf);
localEndpoints.put(newEndpointKey, epList);
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
"default port for server socket factory " + ssf +
" and client socket factory " + csf +
" set to " + port);
}
}
}
@Override
public Subtitle parse(InputStream inputStream, String inputEncoding, long startTimeUs)
throws IOException {
try {
XmlPullParser xmlParser = xmlParserFactory.newPullParser();
xmlParser.setInput(inputStream, inputEncoding);
TtmlSubtitle ttmlSubtitle = null;
LinkedList<TtmlNode> nodeStack = new LinkedList<TtmlNode>();
int unsupportedNodeDepth = 0;
int eventType = xmlParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
TtmlNode parent = nodeStack.peekLast();
if (unsupportedNodeDepth == 0) {
String name = xmlParser.getName();
if (eventType == XmlPullParser.START_TAG) {
if (!isSupportedTag(name)) {
Log.i(TAG, "Ignoring unsupported tag: " + xmlParser.getName());
unsupportedNodeDepth++;
} else {
try {
TtmlNode node = parseNode(xmlParser, parent);
nodeStack.addLast(node);
if (parent != null) {
parent.addChild(node);
}
} catch (ParserException e) {
if (strictParsing) {
throw e;
} else {
Log.e(TAG, "Suppressing parser error", e);
// Treat the node (and by extension, all of its children) as unsupported.
unsupportedNodeDepth++;
}
}
}
} else if (eventType == XmlPullParser.TEXT) {
parent.addChild(TtmlNode.buildTextNode(xmlParser.getText()));
} else if (eventType == XmlPullParser.END_TAG) {
if (xmlParser.getName().equals(TtmlNode.TAG_TT)) {
ttmlSubtitle = new TtmlSubtitle(nodeStack.getLast(), startTimeUs);
}
nodeStack.removeLast();
}
} else {
if (eventType == XmlPullParser.START_TAG) {
unsupportedNodeDepth++;
} else if (eventType == XmlPullParser.END_TAG) {
unsupportedNodeDepth--;
}
}
xmlParser.next();
eventType = xmlParser.getEventType();
}
return ttmlSubtitle;
} catch (XmlPullParserException xppe) {
throw new ParserException("Unable to parse source", xppe);
}
}
public static TCPEndpoint getLocalEndpoint(int port,
RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
{
/*
* Find mapping for an endpoint key to the list of local unique
* endpoints for this client/server socket factory pair (perhaps
* null) for the specific port.
*/
TCPEndpoint ep = null;
synchronized (localEndpoints) {
TCPEndpoint endpointKey = new TCPEndpoint(null, port, csf, ssf);
LinkedList<TCPEndpoint> epList = localEndpoints.get(endpointKey);
String localHost = resampleLocalHost();
if (epList == null) {
/*
* Create new endpoint list.
*/
ep = new TCPEndpoint(localHost, port, csf, ssf);
epList = new LinkedList<TCPEndpoint>();
epList.add(ep);
ep.listenPort = port;
ep.transport = new TCPTransport(epList);
localEndpoints.put(endpointKey, epList);
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
"created local endpoint for socket factory " + ssf +
" on port " + port);
}
} else {
synchronized (epList) {
ep = epList.getLast();
String lastHost = ep.host;
int lastPort = ep.port;
TCPTransport lastTransport = ep.transport;
// assert (localHost == null ^ lastHost != null)
if (localHost != null && !localHost.equals(lastHost)) {
/*
* Hostname has been updated; add updated endpoint
* to list.
*/
if (lastPort != 0) {
/*
* Remove outdated endpoints only if the
* port has already been set on those endpoints.
*/
epList.clear();
}
ep = new TCPEndpoint(localHost, lastPort, csf, ssf);
ep.listenPort = port;
ep.transport = lastTransport;
epList.add(ep);
}
}
}
}
return ep;
}
/**
* Set the port of the (shared) default endpoint object.
* When first created, it contains port 0 because the transport
* hasn't tried to listen to get assigned a port, or if listening
* failed, a port hasn't been assigned from the server.
*/
static void setDefaultPort(int port, RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
{
TCPEndpoint endpointKey = new TCPEndpoint(null, 0, csf, ssf);
synchronized (localEndpoints) {
LinkedList<TCPEndpoint> epList = localEndpoints.get(endpointKey);
synchronized (epList) {
int size = epList.size();
TCPEndpoint lastEp = epList.getLast();
for (TCPEndpoint ep : epList) {
ep.port = port;
}
if (size > 1) {
/*
* Remove all but the last element of the list
* (which contains the most recent hostname).
*/
epList.clear();
epList.add(lastEp);
}
}
/*
* Allow future exports to use the actual bound port
* explicitly (see 6269166).
*/
TCPEndpoint newEndpointKey = new TCPEndpoint(null, port, csf, ssf);
localEndpoints.put(newEndpointKey, epList);
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
"default port for server socket factory " + ssf +
" and client socket factory " + csf +
" set to " + port);
}
}
}
/**
* Set the port of the (shared) default endpoint object.
* When first created, it contains port 0 because the transport
* hasn't tried to listen to get assigned a port, or if listening
* failed, a port hasn't been assigned from the server.
*/
static void setDefaultPort(int port, RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
{
TCPEndpoint endpointKey = new TCPEndpoint(null, 0, csf, ssf);
synchronized (localEndpoints) {
LinkedList<TCPEndpoint> epList = localEndpoints.get(endpointKey);
synchronized (epList) {
int size = epList.size();
TCPEndpoint lastEp = epList.getLast();
for (TCPEndpoint ep : epList) {
ep.port = port;
}
if (size > 1) {
/*
* Remove all but the last element of the list
* (which contains the most recent hostname).
*/
epList.clear();
epList.add(lastEp);
}
}
/*
* Allow future exports to use the actual bound port
* explicitly (see 6269166).
*/
TCPEndpoint newEndpointKey = new TCPEndpoint(null, port, csf, ssf);
localEndpoints.put(newEndpointKey, epList);
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
"default port for server socket factory " + ssf +
" and client socket factory " + csf +
" set to " + port);
}
}
}
private static void addToReversePostOrderListIterative(BasicBlock root, List<BasicBlock> lst) {
LinkedList<BasicBlock> stackNode = new LinkedList<>();
LinkedList<Integer> stackIndex = new LinkedList<>();
Set<BasicBlock> setVisited = new HashSet<>();
stackNode.add(root);
stackIndex.add(0);
while (!stackNode.isEmpty()) {
BasicBlock node = stackNode.getLast();
int index = stackIndex.removeLast();
setVisited.add(node);
List<BasicBlock> lstSuccs = new ArrayList<>(node.getSuccs());
lstSuccs.addAll(node.getSuccExceptions());
for (; index < lstSuccs.size(); index++) {
BasicBlock succ = lstSuccs.get(index);
if (!setVisited.contains(succ)) {
stackIndex.add(index + 1);
stackNode.add(succ);
stackIndex.add(0);
break;
}
}
if (index == lstSuccs.size()) {
lst.add(0, node);
stackNode.removeLast();
}
}
}
/**
* Returns the latest value associated with the key (and returns null if none).
* <p>
* Since null is also a possible value, if you get null as the answer, you need
* to call has(key) to determine whether the key really has a mapping or not.
*/
public V get(K key) {
LinkedList<V> list = map2.get(key);
return (list != null) ? list.getLast() : map1.get(key);
}