Skip to content

Instantly share code, notes, and snippets.

View Kotlin-Native's full-sized avatar

Pierre Liebsch Kotlin-Native

View GitHub Profile
List list = Arrays.asList("foo", "bar");
assertThat(list, is(not(empty())));
assertThat(list.get(0), is("foo"));
assertThat(list, hasItem("bar"));
@RequestScoped
public class MyCdiBean {
private boolean flag = true;
@Inject
private MyService service;
public void doSomething() {
if (!flag) {
service.callMethod();
// The Annotation
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD, METHOD, PARAMETER, TYPE})
@Qualifier
public @interface RunInRequestScope {
}
// The Interceptor for the annotation
Map<String, Object> requestDataStore = new HashMap<String, Object>();
boundRequestContext.associate(requestDataStore);
boundRequestContext.activate();
useRequestScopeInHere();
try {
boundRequestContext.invalidate();
boundRequestContext.deactivate();
} finally {
boundRequestContext.dissociate(requestDataStore);
}
public class MyWebSocketApplication extends WebSocketApplication {
public boolean isApplicationRequest(Request request) {
// determine if this application is responsible for this websocket request (identification by incoming request)
return request.toString().contains("/mywebsocketapp/myspecializedsocket/do.connect");
}
public WebSocket createWebSocket(ProtocolHandler protocolHandler, WebSocketListener... listeners) {
// create a new WebSocket and add it to the list of Sockets hold by this app
WebSocket socket = new MyWebSocket(protocolHandler, listeners);
var messageReceived = function(event) {
var eventData = event.data.toString();
console.log("Received via socket: "+eventData);
};
var socket = new WebSocket("ws://www.example.com/mywebsocketapp/myspecializedsocket/do.connect");
socket.onmessage = messageReceived;
//send text to server
socket.send("testtext");
private BigDecimal processNumberInput(String numberString)
throws ParseException {
Number number = numFormatGerman.parse(numberString);
...
String internalAmountString = numFormatEnglish.format(number);
...
return new BigDecimal(internalAmountString);
}
// Das Attribut 'format' wird als 'ThreadLocal' definiert:
private static ThreadLocal<SimpleDateFormat> format = new ThreadLocal<SimpleDateFormat>();
// Beim ersten Zugriff innerhalb eines Threads wird das Objekt initialisiert
private SimpleDateFormat getFormat() {
if (format.get() == null) {
format.set(new SimpleDateFormat("yyyy-MM-dd"));
}
return format.get();
}
private static SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
...
@Override
public XMLGregorianCalendar unmarshal(String value)throws Exception {
...
Date date=format.parse(value);
GregorianCalendar calendar=new GregorianCalendar();
@XmlJavaTypeAdapter(DateAdapter.class)
protected XMLGregorianCalendar dateOfBirth;