Working with Java Enumerated types (Enums) « Java PitStop
01 enum Speed{
02 LOW(3,2.5){ //Line 1
03 String getInformation(){ //Method overriden by the Enum constants
04 return "Running of Low speed with less units consumed";
05 }
06 },
07 MEDIUM(6,5.0){ //Line 2
08 String getInformation(){
09 return "Running of Medium speed with moderate units consumed";
10 }
11 },
12 HIGH(10,7.5){ //Line 3
13 String getInformation(){
14 return "Running of High speed with Maximum units consumed";
15 }
16 };
17 private int speedValue;
18 private double unitsConsumed;
19 Speed(int speed, double units){
20 this.speedValue=speed;
21 this.unitsConsumed=units;
22 }
23 public int getSpeedValue(){
24 return this.speedValue;
25 }
26 public double getUnitsConsumed(){
27 return this.unitsConsumed;
28 }
29 abstract String getInformation(); //This is an abstract method
30 }
31 public class Machine{
32 Speed machineSpeed;
33 Machine(Speed speed){
34 this.machineSpeed=speed;
35 }
36 Speed getSpeed(){
37 return this.machineSpeed;
38 }
39 public static void main(String[] args){
40 Machine machine1 = new Machine(Speed.LOW);
41 Machine machine2 = new Machine(Speed.HIGH);
42 System.out.println("Machine 1 speed: "+machine1.getSpeed().getInformation());
43 System.out.println("Machine 2 speed: "+machine2.getSpeed().getInformation());
44 }
45 }
The busy Java developer's guide to Scala: Scala and servlets
Summary: In order for a language to be recognized as "real-world" and "ready for prime-time," the language has to be able to reach out to real-world environments and applications. In this installment, Ted Neward begins a tour of Scala in the real world by examining how Scala can interact with the core Servlet API and perhaps even improve it a little.
The Java Community Process(SM) Program - JSRs: Java Specification Requests - detail JSR# 316
-
- JSR-196 Java Authentication SPI for Containers
- JSR-236 Timer for Application Servers
- JSR-237 Work Manager for Application Servers
- JSR-299 Web Beans
- JSR-311 JAX-RS: Java API for RESTful Web Services
- Enterprise JavaBeans
- Java Persistence API
- Servlets
- JavaServer Faces
- JAX-WS
- Java EE Connector API
We propose to include the following new JSRs in Java EE 6:
-
- JSR-168 Portlet Specification
- JSR-170 Content Repository for Java technology API
- JSR-207 Process Definition for Java
- JSR-208 Java Business Integration (JBI)
- JSR-225 XQuery API for Java (XQJ)
- JSR-235 Service Data Objects
- JSR-286 Portlet Specification 2.0
- JSR-289 SIP Servlet v1.1
- JSR-301 Portlet Bridge Specification for JavaServer Faces
String Interpolation in Clojure - Chas Emerick - Muck and Brass
(ns commons.clojure.strint
(:use [clojure.contrib.duck-streams :only (slurp*)]))
(defn- silent-read
[s]
(try
(let [r (-> s java.io.StringReader. java.io.PushbackReader.)]
[(read r) (slurp* r)])
(catch Exception e))) ; this indicates an invalid form -- s is just string data
(defn- interpolate
([s atom?]
(lazy-seq
(if-let [[form rest] (silent-read (subs s (if atom? 2 1)))]
(cons form (interpolate (if atom? (subs rest 1) rest)))
(cons (subs s 0 2) (interpolate (subs s 2))))))
([#^String s]
(let [start (max (.indexOf s "~{") (.indexOf s "~("))]
(if (== start -1)
[s]
(lazy-seq (cons
(subs s 0 start)
(interpolate (subs s start) (= \{ (.charAt s (inc start))))))))))
(defmacro <<
[string]
`(str ~@(interpolate string)))
-
The public macro << (named as an homage to heredocs) takes a single string argument, and emits a str invocation that concatenates the string data and evaluated expressions contained within that argument.
-
(<< "There's ~(dec n) bottles of beer on the wall...")
"There's 98 bottles of beer on the wall..." - 6 more annotations...
Groovy Goodness: Using the ServletCategory - Messages from mrhaki
// File: Start.groovy
01.import javax.servlet.http.*
02.import javax.servlet.*
03.import groovy.servlet.ServletCategory
04.
05.class Start extends HttpServlet {
06. def application
07.
08. void init(ServletConfig config) {
09. super.init(config)
10. application = config.servletContext
11. use(ServletCategory) {
12. application.author = 'mrhaki'
13. }
14. }
15.
16. void doGet(HttpServletRequest request, HttpServletRespons reponse) {
17. def session = request.session
18. use (ServletCategory) {
19. if (session.counter) { // We can use . notation to access session attribute.
20. session.counter++ // We can use . notation to set value for session attribute.
21. } else {
22. session.counter = 1
23. }
24.
25. request.pageTitle = 'Groovy Rocks!'
26. }
27. application.getRequestDispatcher('/output').forward(request, response)
28. }
29.}
Sponsored Links
Top Tags
- 383clojure,
- 356java,
- 295programming,
- 236tutorial,
- 200reference,
- 126webdev,
- 120j2ee,
- 118javascript,
- 118analysis,
- 113example,
View All Recent Tags (49)
- 18clojure,
- 16java,
- 14groovy,
- 9goodness,
- 8analysis,
- 8example,
- 7howto,
- 7haskell,
- 7programming,
- 67,
- 6tutorial,
- 5code,
- 5webdev,
- 5spanish,
- 5reference,
- 5interop,
- 5emacs,
- 4data,
- 4j2ee,
- 4discussion,
- 4review,
- 3dynamic,
- 3firefox,
- 3development,
- 3tool,
- 3import,
- 3app,
- 3closure,
- 3transaction,
- 3ibm,
- 3bestpractices,
- 3tdd,
- 3closures,
- 3web,
- 3functional,
- 3intro,
- 2security,
- 2windows,
- 2internet,
- 2manifiesto,
- 2servlet,
- 2grails,
- 2template,
- 2integration,
- 2db2,
- 2google,
- 2call,
- 2opensource,
- 2library
Public Tags (1551)
Javier Neira's Public Lists (4)
Highlighter, Sticky notes, Tagging, Groups and Network: integrated suite dramatically boosting research productivity. Learn more »
Join Diigo