capture text selection in a JTextComponent

public void caretUpdate(CaretEvent e) {
int dot = e.getDot();
int mark = e.getMark();
if (dot == mark) return; // no text selection

if (!(e.getSource() instanceof JTextComponent)) return;
String selection = ((JTextComponent) e.getSource()).getSelectedText();


easiest way to display a DB record in jtable

DefaultTableModel model = new DefaultTableModel(new Object[] {“Col”,”Value” }, 0);

model.addRow(new Object[] {selection});

Map map = new JdbcTemplate().queryForMap(“select * from table1 where …”);

for (Map.Entry entry : map.entrySet()) {
  model.addRow(new Object[] { entry.getKey(),
  entry.getValue() });
this.table.repaint(); // thread safe

Linq, extension method etc – random thoughts

I find the c#3.0 center piece — the LINQ feature — a lot of fancy beautification features and syntactic sugar. I don’t feel they
add any real capability that was completely “not doable” in C# 2.0.

However, Linq is indeed adopted in investment banks.

They certainly make c# source code more elegant, more natural, more like English, more “dense” but I feel in so doing they also add
additional layers of complexity. I don’t know if this is the best direction for a language.

Linq requires extension methods. Compiler magically converts static methods into functions that appear to be instance methods of the
IEnumerable type.

Linq encourage lambda (in place of plain old delegates). Compiler converts each lambda expression into a delegate instance.

Linq encourages anonymous types. Compiler converts each such type to a real type whose generated name is known only to the compiler.

Q: What problems do you have with Linq?
A(from a veteran): slow for SQL

interpolating on vol surface between tenors #worked eg

Q: how do you query the vol surface at a fitted strike but between 2 fitted maturities — Jun and next Dec, assuming today is Jan 1.

First Take sigma_J*sigma_J and sigma_D*sigma_D. Say we get Something like 20%^2 and 30%^2. Remember these are annualized sigmas. Suppose those maturities are 6 months and 24 months out. Raw variance values would be

Variance_J = (20%^2)* 6m/12m = .02
Variance_D = (30%^2)* 24m/12m = .18

Our assumption is that variance is linear with TTL, so let’s line up our raw variance values

6 months to expiry –> .02
15 months to expiry -> x
24 months to expiry -> .18

==> x = .10 (not annualized)

Annualized variance_x == x /(15/12) = .08
Annualized sigma_x = 28.28%

This estimate is better than a naïve linear interpolation like

6 -> 20%
15 -> ?????? — 25%
24 -> 30%

delegate invocation list, briefly

— based on
An invocation list is a reverse singly linked list, with the last appended “single” as head. Like in hash table buckets, this is optimized for insertion. 

Invocation time — Since this is a singly linked list, we need recursion. Head node’s method (last appended) starts first, invoking 2nd-last-appended.

When you append [d1,d2, d3, d4] to an existing invocation list, system effectively adds d1, then adds d2… (Internally, it could do it faster with pointers.), so I call these d1, d2 .. items “single-method delegates”, or “singles”.

Note the link pointer between link nodes is named “PreviousNode”.

Remove() is by logical-equality, not by reference equality.

##implicit compiler acts on delegates

— Based on

# MyDelegate d1 = new MyDelegate(this.InstanceMethod1); // when you add this code in a non-static context, the “this.” is implicited added by compiler, but you can add it explicitly

# MyDelegate d2 = new MyDelegate(Class4.StaticMethod2); // the class name, if omitted, is inferred by compiler

# when you call d1(someArg), compiler implicitly calls d1.Invoke(someArg). You do not have to call this method explicitly. Invoke() is useful in reflection. See MSDN

# when you perform d3 = d1 + d2, compiler implicit calls the static Delegate.Combine(d1, d2)
# when you perofrm d3 – d2, compiler implicitly calls Delegate.Remove(d3, d2)

specify a functor-object^functor-type

Sometimes [1] you plug in a functor object; sometimes [2] you plug in a functor type into a template.

In [2], I confirmed that concretized class’s would instantiate a functor object. In short, you specify functor TYPE only– instantiation is implicit. Note a template usually specifies a type param. A function type meets the requirement; a functor object doesn’t.

(I seldom see a template with a NDTTP like “int” — kind of wizardry.)

[1] In the first case you usually have a regular function, not a template

One use case of functor-object-passing (pbclone) is a STL algorithm like

 class myLess {/*....*/};

 std::sort(V.begin(), V.end(), myLess()); // which is short version of

 std::sort <vector ::iterator, // 1st concrete type
               myLess // 2nd concrete type
           > //now we have a concretized function. Below are 3 args:
   (V.begin(), V.end(), myLess()); // ctor args

In this case, the functor instantiation is fairly explicit.

discount curve – cheatsheet

Based on P245 of [[complete guide to capital markets]]

Discount curve is designed to tells us how to discount to NPV $1 received x days from today, where x can be 1 to 365 * 30. If the curve value for Day 365 is 0.80, then SPOT rate is 25% or 2500bps — 80 cents invested today becomes $1. Note the value on the curve is not bps or spot rate, but a discount factor value from 0 to 1.

Q: how do I get forward rate between any 2 dates?
A: P246. Simple formula.

Discount curve is “built” from and must be consistent with
+ ED CD rates (spot rates) of 1,7,14..,90 days. As loans, these loan term always starts today.
+ ED futures rates (forward rates). Loan term always last 3 months, but start on the 4 IMM dates of this year, next year, next next year ….
(Note ED futures rates are determined on the market; ED CD rates are announced by BBA.)

(Note I have ignored IR swaps, which are more liquid than ED futures beyond 10Y tenor.)

Discount curve needs to cover every single date. First couple of months are covered by latest announced ED CD rates, interpolating when necessary. After we pass 90, all subsequent dates (up to 30 years) are covered by ED futures rates observed on CME. Being forward rates, these aren’t directly usable as those CD rates, but still very simple — If the 3-month forward rate 3/19/2008 – 6/19/2008 is 200bps, and discount factor for 3/19 is 0.9, then 6/19 discount factor is (0.9 / 1.02)

##hard CORE java/c++ skills across domains

— c++
void ptr
double pointer
ptr to ref; ref to ptr
ptr to method
new/delete global overload to intercept new/delete operations
scoped typedef, namespace
static initialization sequence

Note: MOM, spring, hibernate, data grid … aren’t core java and out of scope.


— Below are needed only in rare situations, IMHO
memory management — leak detectors, garbage collection, weak (soft) references, memory profilers,

jdk and cglib proxy
custom class loaders
byte code engineering
JVMTI — (eg jprobe) powerful tools to analyze production systems.
real time java

memcached – a few tips from Facebook

based on

* distributed hash map at heart
* sits between apache web server and mysql. Intercepts requests to mysql
* onMsg() updates to clients? I don’t think so.
* TCP between memcached and apache. Each TCP connection occupied memory, so if there are 1000 web hosts, each running 100 Apache processes, you can get 400,000 TCP connections open to memcached. They occupy 5GB memory. Solution is UDP — connection-less.