java generics & algebra for fun