下面列出了java.util.LinkedList#getFirst ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static StatsSnapshot computeStatsData(final LinkedList<CallSnapshot> csList) {
StatsSnapshot statsSnapshot = new StatsSnapshot();
synchronized (csList) {
double tps = 0;
double avgpt = 0;
long sum = 0;
if (!csList.isEmpty()) {
CallSnapshot first = csList.getFirst();
CallSnapshot last = csList.getLast();
sum = last.getValue() - first.getValue();
tps = (sum * 1000.0d) / (last.getTimestamp() - first.getTimestamp());
long timesDiff = last.getTimes() - first.getTimes();
if (timesDiff > 0) {
avgpt = (sum * 1.0d) / timesDiff;
}
}
statsSnapshot.setSum(sum);
statsSnapshot.setTps(tps);
statsSnapshot.setAvgpt(avgpt);
}
return statsSnapshot;
}
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) {
String traceid = TraceUtil.getTraceId();
Determine determine = ExceptionUtil.determineType(ex);
log.error("TraceId[{}]发生异常[{}]", traceid, determine.getName(), ex);
ModelAndView mv = new ModelAndView(view);
Response<?> result = null;
LinkedList<Stack> stacks = ExceptionUtil.getStacks(ex, app, traceid);
Stack stack = stacks.getFirst();
Map<String, Object> data = new HashMap<>();
data.put("traceid", traceid);
data.put("stacks", stacks);
result = Response.err(stack.getCode(), stack.getMessage(), data);
mv.setStatus(determine.getStatus());
return mv.addObject(result);
}
/**
* Returns the current list of known transports.
* The returned list is an unshared collection of Transports,
* including all transports which may have channels to remote
* endpoints.
*/
private static Collection<TCPTransport> allKnownTransports() {
// Loop through local endpoints, getting the transport of each one.
Set<TCPTransport> s;
synchronized (localEndpoints) {
// presize s to number of localEndpoints
s = new HashSet<TCPTransport>(localEndpoints.size());
for (LinkedList<TCPEndpoint> epList : localEndpoints.values()) {
/*
* Each local endpoint has its transport added to s.
* Note: the transport is the same for all endpoints
* in the list, so it is okay to pick any one of them.
*/
TCPEndpoint ep = epList.getFirst();
s.add(ep.transport);
}
}
return s;
}
public boolean updatePoint(final String lockID) throws Exception {
final Pair<CloseableHttpClient, HttpClientContext> clientAndContext = createClientAndContext();
final CloseableHttpClient httpclient = clientAndContext.getLeft();
final HttpClientContext context = clientAndContext.getRight();
try {
final HttpPost command = createWFSTransaction(httpclient, "1.1.0");
command.setEntity(new StringEntity(update));
final LinkedList<HttpResponse> capturedResponse = new LinkedList<>();
run(new Runnable() {
@Override
public void run() {
try {
capturedResponse.add(httpclient.execute(command, context));
} catch (final Exception e) {
throw new RuntimeException("update point client failed", e);
}
}
}, 500000);
final HttpResponse r = capturedResponse.getFirst();
return r.getStatusLine().getStatusCode() == Status.OK.getStatusCode();
} finally {
httpclient.close();
}
}
/**
* Returns the current list of known transports.
* The returned list is an unshared collection of Transports,
* including all transports which may have channels to remote
* endpoints.
*/
private static Collection<TCPTransport> allKnownTransports() {
// Loop through local endpoints, getting the transport of each one.
Set<TCPTransport> s;
synchronized (localEndpoints) {
// presize s to number of localEndpoints
s = new HashSet<TCPTransport>(localEndpoints.size());
for (LinkedList<TCPEndpoint> epList : localEndpoints.values()) {
/*
* Each local endpoint has its transport added to s.
* Note: the transport is the same for all endpoints
* in the list, so it is okay to pick any one of them.
*/
TCPEndpoint ep = epList.getFirst();
s.add(ep.transport);
}
}
return s;
}
public static void writeStr(LinkedList<Integer> seq, StringBuilder sb, int min, int max) {
if (seq.size()>2) {
int diff=seq.get(1)-seq.getFirst();
int extendLeft=seq.getFirst()-diff;
if (!notPrime[extendLeft] && primeIndex[seq.getFirst()]-1==primeIndex[extendLeft] && extendLeft<min) return;
int extendRight=seq.getLast()+diff;
if (!notPrime[extendRight] && primeIndex[seq.getLast()]+1==primeIndex[extendRight] && extendRight>max) return;
for (int n : seq) {
sb.append(n);
sb.append(' ');
}
sb.setLength(sb.length()-1);
sb.append('\n');
}
}
@Override
SingleDynamicMethod getMethodForExactParamTypes(String paramTypes) {
final LinkedList<SingleDynamicMethod> matchingMethods = new LinkedList<>();
for(SingleDynamicMethod method: methods) {
final SingleDynamicMethod matchingMethod = method.getMethodForExactParamTypes(paramTypes);
if(matchingMethod != null) {
matchingMethods.add(matchingMethod);
}
}
switch(matchingMethods.size()) {
case 0: {
return null;
}
case 1: {
return matchingMethods.getFirst();
}
default: {
throw new BootstrapMethodError("Can't choose among " + matchingMethods + " for argument types "
+ paramTypes + " for method " + getName());
}
}
}
@Override
SingleDynamicMethod getMethodForExactParamTypes(final String paramTypes) {
final LinkedList<SingleDynamicMethod> matchingMethods = new LinkedList<>();
for(final SingleDynamicMethod method: methods) {
final SingleDynamicMethod matchingMethod = method.getMethodForExactParamTypes(paramTypes);
if(matchingMethod != null) {
matchingMethods.add(matchingMethod);
}
}
switch(matchingMethods.size()) {
case 0: {
return null;
}
case 1: {
return matchingMethods.getFirst();
}
default: {
throw new BootstrapMethodError("Can't choose among " + matchingMethods + " for argument types "
+ paramTypes + " for method " + getName());
}
}
}
private static StatsSnapshot computeStatsData(final LinkedList<CallSnapshot> csList) {
StatsSnapshot statsSnapshot = new StatsSnapshot();
synchronized (csList) {
double tps = 0;
double avgpt = 0;
long sum = 0;
if (!csList.isEmpty()) {
CallSnapshot first = csList.getFirst();
CallSnapshot last = csList.getLast();
sum = last.getValue() - first.getValue();
tps = (sum * 1000.0d) / (last.getTimestamp() - first.getTimestamp());
long timesDiff = last.getTimes() - first.getTimes();
if (timesDiff > 0) {
avgpt = (sum * 1.0d) / (timesDiff);
}
}
statsSnapshot.setSum(sum);
statsSnapshot.setTps(tps);
statsSnapshot.setAvgpt(avgpt);
}
return statsSnapshot;
}
public int[] maxSlidingWindow(int[] nums, int k) {
int len = nums.length;
if (len == 0) {
return new int[0];
}
int[] res = new int[len - k + 1];
LinkedList<Integer> queue = new LinkedList<>();
for (int i = 0; i < len; i++) {
if (i >= k && queue.getFirst() == i - k) {
queue.removeFirst();
}
while (!queue.isEmpty() && nums[queue.getLast()] <= nums[i]) {
queue.removeLast();
}
queue.add(i);
if (i >= k - 1) {
res[i - k + 1] = nums[queue.getFirst()];
}
}
return res;
}
public String getURI(String prefix) {
String uri = null;
LinkedList<String> stack = xmlPrefixMapper.get(prefix);
if (stack == null || stack.size() == 0) {
uri = jspPrefixMapper.get(prefix);
} else {
uri = stack.getFirst();
}
return uri;
}
public String getURI(String prefix) {
String uri = null;
LinkedList<String> stack = xmlPrefixMapper.get(prefix);
if (stack == null || stack.size() == 0) {
uri = jspPrefixMapper.get(prefix);
} else {
uri = stack.getFirst();
}
return uri;
}
@Override
protected void processArguments(LinkedList<PathData> items)
throws IOException {
super.processArguments(items);
if (numErrors != 0) { // check for error collecting paths
return;
}
assert(items.size() == 1);
PathData sroot = items.getFirst();
Path snapshotPath = sroot.fs.createSnapshot(sroot.path, snapshotName);
out.println("Created snapshot " + snapshotPath);
}
/**
* Test csm banner timeout set on Console
*
* @throws Exception
*/
@Test
public void testBannerCSMResponseNonZeroTimeout() throws Exception {
String bannerCSMString = TestResponsesUT.mediatedSuccessfulBannerTimeoutNonZero();
utAdResponse = new UTAdResponse(bannerCSMString, null, MediaType.BANNER, "v");
assertNotNull(utAdResponse);
LinkedList<BaseAdResponse> list = utAdResponse.getAdList();
assertNotNull(utAdResponse.getAdList());
CSMSDKAdResponse baseCSMSDKAdResponse = (CSMSDKAdResponse) list.getFirst();
assertEquals("csm", baseCSMSDKAdResponse.getContentSource());
assertEquals(200, baseCSMSDKAdResponse.getNetworkTimeout());
}
/**
* Вовзращаем первй элемент списка.
* Из списка этот элемент будет удален
*/
private static <T> T getAndRemoveFirst(LinkedList<T> list) {
if (list == null || list.isEmpty())
return null;
T first = list.getFirst();
list.removeFirst();
return first;
}
public static void main (String [] args) throws Exception {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s;
while (!(s=br.readLine()).equals("0")) {
int N=Integer.parseInt(s);
StringTokenizer st=new StringTokenizer(br.readLine());
LinkedList<House> sell=new LinkedList<>();
LinkedList<House> buy=new LinkedList<>();
for (int n=0;n<N;n++) {
House h=new House(n,Integer.parseInt(st.nextToken()));
if (h.value<0) sell.add(h);
else if (h.value>0) buy.add(h);
}
long ans=0;
while (!sell.isEmpty() && !buy.isEmpty()) {
House firstSell=sell.getFirst();
House firstBuy=buy.getFirst();
long v=Math.min(-firstSell.value,firstBuy.value);
firstSell.value+=v;
firstBuy.value-=v;
ans+=(v*Math.abs(firstSell.index-firstBuy.index));
if (firstSell.value==0) sell.removeFirst();
if (firstBuy.value==0) buy.removeFirst();
}
System.out.println(ans);
}
}
/**
* Test csm native server timeout
*
* @throws Exception
*/
@Test
public void testNativeCSMResponse() throws Exception {
String bannerCSMString = TestResponsesUT.mediatedSuccessfulNative();
utAdResponse = new UTAdResponse(bannerCSMString, null, MediaType.BANNER, "v");
assertNotNull(utAdResponse);
LinkedList<BaseAdResponse> list = utAdResponse.getAdList();
assertNotNull(utAdResponse.getAdList());
CSMSDKAdResponse baseCSMSDKAdResponse = (CSMSDKAdResponse) list.getFirst();
assertEquals("csm", baseCSMSDKAdResponse.getContentSource());
assertEquals(15000, baseCSMSDKAdResponse.getNetworkTimeout());
}
private int peekStatus(LinkedList statusStack) {
if (statusStack.size() == 0)
return -1;
Integer status = (Integer) statusStack.getFirst();
return status.intValue();
}
private int peekStatus(LinkedList statusStack){
if(statusStack.size()==0)
return -1;
Integer status=(Integer)statusStack.getFirst();
return status.intValue();
}
private int peekStatus(LinkedList statusStack){
if(statusStack.size()==0)
return -1;
Integer status=(Integer)statusStack.getFirst();
return status.intValue();
}