소스 파일 최초 업로드
This commit is contained in:
Binary file not shown.
BIN
tomcat/webapps.dist/examples/WEB-INF/classes/async/Async0.class
Normal file
BIN
tomcat/webapps.dist/examples/WEB-INF/classes/async/Async0.class
Normal file
Binary file not shown.
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package async;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.juli.logging.Log;
|
||||
import org.apache.juli.logging.LogFactory;
|
||||
|
||||
public class Async0 extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private static final Log log = LogFactory.getLog(Async0.class);
|
||||
|
||||
@Override
|
||||
protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
|
||||
if (Boolean.TRUE.equals(req.getAttribute("dispatch"))) {
|
||||
log.info("Received dispatch, completing on the worker thread.");
|
||||
log.info("After complete called started:"+req.isAsyncStarted());
|
||||
Date date = new Date(System.currentTimeMillis());
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
|
||||
resp.getWriter().write("Async dispatch worked: " + sdf.format(date) + "\n");
|
||||
} else {
|
||||
resp.setContentType("text/plain");
|
||||
final AsyncContext actx = req.startAsync();
|
||||
actx.setTimeout(Long.MAX_VALUE);
|
||||
Runnable run = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
req.setAttribute("dispatch", Boolean.TRUE);
|
||||
Thread.currentThread().setName("Async0-Thread");
|
||||
log.info("Putting AsyncThread to sleep");
|
||||
Thread.sleep(2*1000);
|
||||
log.info("Dispatching");
|
||||
actx.dispatch();
|
||||
}catch (InterruptedException | IllegalStateException x) {
|
||||
log.error("Async1",x);
|
||||
}
|
||||
}
|
||||
};
|
||||
Thread t = new Thread(run);
|
||||
t.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
BIN
tomcat/webapps.dist/examples/WEB-INF/classes/async/Async1.class
Normal file
BIN
tomcat/webapps.dist/examples/WEB-INF/classes/async/Async1.class
Normal file
Binary file not shown.
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package async;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.juli.logging.Log;
|
||||
import org.apache.juli.logging.LogFactory;
|
||||
|
||||
public class Async1 extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private static final Log log = LogFactory.getLog(Async1.class);
|
||||
|
||||
@Override
|
||||
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
final AsyncContext actx = req.startAsync();
|
||||
actx.setTimeout(30*1000);
|
||||
Runnable run = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
String path = "/jsp/async/async1.jsp";
|
||||
Thread.currentThread().setName("Async1-Thread");
|
||||
log.info("Putting AsyncThread to sleep");
|
||||
Thread.sleep(2*1000);
|
||||
log.info("Dispatching to "+path);
|
||||
actx.dispatch(path);
|
||||
}catch (InterruptedException | IllegalStateException x) {
|
||||
log.error("Async1",x);
|
||||
}
|
||||
}
|
||||
};
|
||||
Thread t = new Thread(run);
|
||||
t.start();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Binary file not shown.
BIN
tomcat/webapps.dist/examples/WEB-INF/classes/async/Async2.class
Normal file
BIN
tomcat/webapps.dist/examples/WEB-INF/classes/async/Async2.class
Normal file
Binary file not shown.
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package async;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.juli.logging.Log;
|
||||
import org.apache.juli.logging.LogFactory;
|
||||
|
||||
public class Async2 extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private static final Log log = LogFactory.getLog(Async2.class);
|
||||
|
||||
@Override
|
||||
protected void service(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException {
|
||||
final AsyncContext actx = req.startAsync();
|
||||
actx.setTimeout(30*1000);
|
||||
Runnable run = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.currentThread().setName("Async2-Thread");
|
||||
log.info("Putting AsyncThread to sleep");
|
||||
Thread.sleep(2*1000);
|
||||
log.info("Writing data.");
|
||||
Date date = new Date(System.currentTimeMillis());
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
|
||||
actx.getResponse().getWriter().write(
|
||||
"Output from background thread. Time: " + sdf.format(date) + "\n");
|
||||
actx.complete();
|
||||
}catch (InterruptedException | IllegalStateException | IOException x) {
|
||||
log.error("Async2",x);
|
||||
}
|
||||
}
|
||||
};
|
||||
Thread t = new Thread(run);
|
||||
t.start();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
BIN
tomcat/webapps.dist/examples/WEB-INF/classes/async/Async3.class
Normal file
BIN
tomcat/webapps.dist/examples/WEB-INF/classes/async/Async3.class
Normal file
Binary file not shown.
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package async;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
public class Async3 extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
final AsyncContext actx = req.startAsync();
|
||||
actx.setTimeout(30*1000);
|
||||
actx.dispatch("/jsp/async/async3.jsp");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package async;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
/*
|
||||
* Ensures the Stockticker is shut down cleanly when the context stops. This
|
||||
* also covers the case when the server shuts down.
|
||||
*/
|
||||
public class AsyncStockContextListener implements ServletContextListener {
|
||||
|
||||
public static final String STOCK_TICKER_KEY = "StockTicker";
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
Stockticker stockticker = new Stockticker();
|
||||
ServletContext sc = sce.getServletContext();
|
||||
sc.setAttribute(STOCK_TICKER_KEY, stockticker);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
ServletContext sc = sce.getServletContext();
|
||||
Stockticker stockticker = (Stockticker) sc.getAttribute(STOCK_TICKER_KEY);
|
||||
stockticker.shutdown();
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package async;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.AsyncEvent;
|
||||
import javax.servlet.AsyncListener;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.juli.logging.Log;
|
||||
import org.apache.juli.logging.LogFactory;
|
||||
|
||||
import async.Stockticker.Stock;
|
||||
import async.Stockticker.TickListener;
|
||||
|
||||
public class AsyncStockServlet extends HttpServlet implements TickListener, AsyncListener{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private static final Log log = LogFactory.getLog(AsyncStockServlet.class);
|
||||
|
||||
private static final ConcurrentLinkedQueue<AsyncContext> clients =
|
||||
new ConcurrentLinkedQueue<>();
|
||||
private static final AtomicInteger clientcount = new AtomicInteger(0);
|
||||
|
||||
public AsyncStockServlet() {
|
||||
log.info("AsyncStockServlet created");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void service(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException {
|
||||
if (req.isAsyncStarted()) {
|
||||
req.getAsyncContext().complete();
|
||||
} else if (req.isAsyncSupported()) {
|
||||
AsyncContext actx = req.startAsync();
|
||||
actx.addListener(this);
|
||||
resp.setContentType("text/plain");
|
||||
clients.add(actx);
|
||||
if (clientcount.incrementAndGet()==1) {
|
||||
Stockticker ticker = (Stockticker) req.getServletContext().getAttribute(
|
||||
AsyncStockContextListener.STOCK_TICKER_KEY);
|
||||
ticker.addTickListener(this);
|
||||
}
|
||||
} else {
|
||||
new Exception("Async Not Supported").printStackTrace();
|
||||
resp.sendError(400,"Async is not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void tick(Stock stock) {
|
||||
for (AsyncContext actx : clients) {
|
||||
try {
|
||||
writeStock(actx, stock);
|
||||
} catch (Exception e) {
|
||||
// Ignore. The async error handling will deal with this.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void writeStock(AsyncContext actx, Stock stock) throws IOException {
|
||||
HttpServletResponse response = (HttpServletResponse)actx.getResponse();
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.write("STOCK#");//make client parsing easier
|
||||
writer.write(stock.getSymbol());
|
||||
writer.write("#");
|
||||
writer.write(stock.getValueAsString());
|
||||
writer.write("#");
|
||||
writer.write(stock.getLastChangeAsString());
|
||||
writer.write("#");
|
||||
writer.write(String.valueOf(stock.getCnt()));
|
||||
writer.write("\n");
|
||||
writer.flush();
|
||||
response.flushBuffer();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
// The web application is shutting down. Complete any AsyncContexts
|
||||
// associated with an active client.
|
||||
for (AsyncContext actx : clients) {
|
||||
try {
|
||||
actx.complete();
|
||||
} catch (Exception e) {
|
||||
// Ignore. The async error handling will deal with this.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onComplete(AsyncEvent event) throws IOException {
|
||||
if (clients.remove(event.getAsyncContext()) && clientcount.decrementAndGet()==0) {
|
||||
ServletContext sc = event.getAsyncContext().getRequest().getServletContext();
|
||||
Stockticker ticker = (Stockticker) sc.getAttribute(
|
||||
AsyncStockContextListener.STOCK_TICKER_KEY);
|
||||
ticker.removeTickListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(AsyncEvent event) throws IOException {
|
||||
event.getAsyncContext().complete();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimeout(AsyncEvent event) throws IOException {
|
||||
event.getAsyncContext().complete();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onStartAsync(AsyncEvent event) throws IOException {
|
||||
// NOOP
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,212 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package async;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class Stockticker implements Runnable {
|
||||
public volatile boolean run = true;
|
||||
protected final AtomicInteger counter = new AtomicInteger(0);
|
||||
final List<TickListener> listeners = new CopyOnWriteArrayList<>();
|
||||
protected volatile Thread ticker = null;
|
||||
protected volatile int ticknr = 0;
|
||||
|
||||
public synchronized void start() {
|
||||
run = true;
|
||||
ticker = new Thread(this);
|
||||
ticker.setName("Ticker Thread");
|
||||
ticker.start();
|
||||
}
|
||||
|
||||
public synchronized void stop() {
|
||||
// On context stop this can be called multiple times.
|
||||
// NO-OP is the ticker thread is not set
|
||||
// (i.e. stop() has already completed)
|
||||
if (ticker == null) {
|
||||
return;
|
||||
}
|
||||
run = false;
|
||||
try {
|
||||
ticker.join();
|
||||
}catch (InterruptedException x) {
|
||||
Thread.interrupted();
|
||||
}
|
||||
|
||||
ticker = null;
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
// Notify each listener of the shutdown. This enables them to
|
||||
// trigger any necessary clean-up.
|
||||
for (TickListener l : listeners) {
|
||||
l.shutdown();
|
||||
}
|
||||
// Wait for the thread to stop. This prevents warnings in the logs
|
||||
// that the thread is still active when the context stops.
|
||||
stop();
|
||||
}
|
||||
|
||||
public void addTickListener(TickListener listener) {
|
||||
if (listeners.add(listener)) {
|
||||
if (counter.incrementAndGet()==1) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void removeTickListener(TickListener listener) {
|
||||
if (listeners.remove(listener)) {
|
||||
if (counter.decrementAndGet()==0) {
|
||||
stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
|
||||
Stock[] stocks = new Stock[] { new Stock("GOOG", 435.43),
|
||||
new Stock("YHOO", 27.88), new Stock("ASF", 1015.55), };
|
||||
Random r = new Random(System.currentTimeMillis());
|
||||
while (run) {
|
||||
for (int j = 0; j < 1; j++) {
|
||||
int i = r.nextInt() % 3;
|
||||
if (i < 0) {
|
||||
i = i * (-1);
|
||||
}
|
||||
Stock stock = stocks[i];
|
||||
double change = r.nextDouble();
|
||||
boolean plus = r.nextBoolean();
|
||||
if (plus) {
|
||||
stock.setValue(stock.getValue() + change);
|
||||
} else {
|
||||
stock.setValue(stock.getValue() - change);
|
||||
}
|
||||
stock.setCnt(++ticknr);
|
||||
for (TickListener l : listeners) {
|
||||
l.tick(stock);
|
||||
}
|
||||
|
||||
}
|
||||
Thread.sleep(850);
|
||||
}
|
||||
} catch (InterruptedException ix) {
|
||||
// Ignore
|
||||
} catch (Exception x) {
|
||||
x.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface TickListener {
|
||||
void tick(Stock stock);
|
||||
void shutdown();
|
||||
}
|
||||
|
||||
public static final class Stock implements Cloneable {
|
||||
protected static final DecimalFormat df = new DecimalFormat("0.00");
|
||||
protected final String symbol;
|
||||
protected double value = 0.0d;
|
||||
protected double lastchange = 0.0d;
|
||||
protected int cnt = 0;
|
||||
|
||||
public Stock(String symbol, double initvalue) {
|
||||
this.symbol = symbol;
|
||||
this.value = initvalue;
|
||||
}
|
||||
|
||||
public void setCnt(int c) {
|
||||
this.cnt = c;
|
||||
}
|
||||
|
||||
public int getCnt() {
|
||||
return cnt;
|
||||
}
|
||||
|
||||
public String getSymbol() {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
public double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(double value) {
|
||||
double old = this.value;
|
||||
this.value = value;
|
||||
this.lastchange = value - old;
|
||||
}
|
||||
|
||||
public String getValueAsString() {
|
||||
return df.format(value);
|
||||
}
|
||||
|
||||
public double getLastChange() {
|
||||
return this.lastchange;
|
||||
}
|
||||
|
||||
public void setLastChange(double lastchange) {
|
||||
this.lastchange = lastchange;
|
||||
}
|
||||
|
||||
public String getLastChangeAsString() {
|
||||
return df.format(lastchange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return symbol.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other instanceof Stock) {
|
||||
return this.symbol.equals(((Stock) other).symbol);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder buf = new StringBuilder("STOCK#");
|
||||
buf.append(getSymbol());
|
||||
buf.append('#');
|
||||
buf.append(getValueAsString());
|
||||
buf.append('#');
|
||||
buf.append(getLastChangeAsString());
|
||||
buf.append('#');
|
||||
buf.append(String.valueOf(getCnt()));
|
||||
return buf.toString();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object clone() {
|
||||
Stock s = new Stock(this.getSymbol(), this.getValue());
|
||||
s.setLastChange(this.getLastChange());
|
||||
s.setCnt(this.cnt);
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user