Categories
discuss

What function do these Java annotations serve?

I’m still not clear on the purpose of annotations in Java. Initially I thought they just served as documentation. But looking at this documentation from Google App Engine Datastore, I’m not so sure. @PersistenceCapable(identityType = IdentityType.APPLICATION) looks more like a method signature.

What’s the purpose of this type of annotation? What does it do?

import java.util.Date;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Employee {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;

    @Persistent
    private String firstName;

    @Persistent
    private String lastName;

    @Persistent
    private Date hireDate;

    public Employee(String firstName, String lastName, Date hireDate) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.hireDate = hireDate;
    }

    // Accessors for the fields.  JDO doesn't use these, but your application does.

    public Long getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    } 

    // ... other accessors...
}

Answer

They’re source-level metadata. They’re a way of adding information to the code that’s not code, and that is easily machine-processed.

In your example, they’re used to configure object-relational mapping for that entity type. It’s saying that for example the id field should be the primary key for that object, and that firstName, lastName, and hireDate should be stored in the database. (To tell those fields apart from some transient object state.)

The GAE support for JDO needs to know what objects you’ll try to store in the database. It does this by looking through the classes in your code, looking for the ones that are annotated with @PersistenceCapable.

Commonly, they’re used to replace where you’d use external configuration files before; the Java standard library has tools to read the annotations in your code, which makes them much easier to process than rolling your own configuration file plumbing, and gets you IDE support for free.

Categories
discuss

How to check whether the elements of an ArrayList are all contained in another ArrayList

How can I easily check to see whether all the elements in one ArrayList are all elements of another ArrayList?

Answer

Use Collection.containsAll():

boolean isSubset = listA.containsAll(listB);
Categories
discuss

javascript to check when the browser window is closed

Does anyone know any way that I can use javascript to check when the browser window is closed and pop-up a confirmation dialog to ask whether the user is confirm to exit the browser or change his mind to stay?

Answer

window.onbeforeunload = function (e) {
  var e = e || window.event;

  //IE & Firefox
  if (e) {
    e.returnValue = 'Are you sure?';
  }

  // For Safari
  return 'Are you sure?';
};

https://developer.mozilla.org/en/DOM/window.onbeforeunload

Categories
discuss

Loading up a web.xml for integration tests with jetty

OK this is kind of related to : Using jetty to install and run servlet tests programmatically

got great answers there, and have been able to load up servlets programmatically and its all made of awesome.

What I would like to do however is load up a web.xml in a test (all in the classpath) and have it run up a server (using the current classpath) – I have seen in docs how to point it to a directory to do that, but I want to work off the classpath (better for in place testing). Essentially validating my web.xml.

(its not relevant, but this app is in scala, but I have had no issue with that, everything works as advertised).

Answer

It sounds like what you want to do is load a proper web application programatically, as opposed to loading individual servlets (and I think you want to do it without having a full WAR file to work from).

Server server = new Server( port );
WebAppContext root = new WebAppContext();

root.setWar("/path/to/somewhere");
root.setContextPath("/");

server.addHandler( root );
server.start();

The trick is that the /path/to/somewhere should contain a WEB-INF directory and your web.xml file should live inside there. Nothing else needs to live within that directory structure, as everything else can be automatically loaded from your classpath (though if you wanted to, you could make that a path to an actual WAR file or complete exploded WAR tree).

Categories
discuss

Creating multiline strings in JavaScript

I have the following code in Ruby. I want to convert this code into JavaScript. What is the equivalent code in JS?

text = <<"HERE"
This
Is
A
Multiline
String
HERE

Answer

Update:

ECMAScript 6 (ES6) introduces a new type of literal, namely template literals. They have many features, variable interpolation among others, but most importantly for this question, they can be multiline.

A template literal is delimited by backticks:

var html = `
  <div>
    <span>Some HTML here</span>
  </div>
`;

(Note: I’m not advocating to use HTML in strings)

Browser support is OK, but you can use transpilers to be more compatible.


Original ES5 answer:

Javascript doesn’t have a here-document syntax. You can escape the literal newline, however, which comes close:

"foo 
bar"
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..