sort a Set

Q: How to sort an instance of java.util.Set?

A:

See a more detailed explanation here.

run a backtest using the JForex API (v. 2.x)

  1. Create a client instance
  2. Register event listeners
  3. Connect the client
  4. Configure the time period to backtest
  5. Configure the instrument
  6. Start the Backtest
Get more info here: Dukaskopy Knowledge Base

maintain library project for Maven and Eclipse RCP/ OSGi in the same repository

If you are working both in the Maven and the Eclipse RCP/ OSGi universe, you might find it quite frustrating that these two worlds cooperate so poorly. In order to use the same library in both worlds, a lot of error-prone copy-and-paste is needed, at least if you would like to use this project in a Maven and Maven Tycho build.

In principle, there is not much that is different for a Maven artifact and an Eclipse RCP/ OSGi Plug-in:
In an Eclipse RCP/ OSGi Plug-in you just need an additional META-INF/MANIFEST.MF and build.properties file. The problem comes with the Maven pom file, which looks completely different for standard Maven and Maven Tycho. Luckily, you can specify the name of the pom file when invoking Maven:

Therefore, you can just maintain two pom files and with only few limitations, it is possible to maintain a library that can be used in a standard Maven build and in a Maven Tycho build within the same repository:

  1. Modify your .gitignore so that it contains the following lines:
    bin
    .project
    .settings
    .classpath
    
    We don’t want to have any Eclipse project configuration information in the project, since we want to be able to import it both as ‘Maven project’ and ‘Eclipse/ OSGi project’.
  2. Remove all files from the git index that match the pattern above.
    • If your project is a Maven project, right-click on the project and select ‘Configure -> Convert to Plug-in project’. This will create the META-INF/MANIFEST.MF and build.properties files.
    • If your project is a Plug-in project, right-click on the project and select ‘Configure -> Convert to Maven project’. This will create the pom.xml file.
    These steps are optional and only needed if you are missing either pom.xml or META-INF/MANIFEST.MF and build.properties .
  3. Create the Tycho pom file, name it e.g. pom-tycho.xml. It’s content should look like this:
  4. Commit all changes and delete the project from your workspace.
  5. Import the project using the ‘Import Existing Maven Project’ or ‘Checkout Maven Project from scm’ wizard. To use the project as an Eclipse RCP/ OSGi Plug-in project, disable it’s Maven nature via ‘Maven -> disable Maven nature’.
  6. Done!
Note: You manually need to keep in sync the version strings in pom.xml, META-INF/MANIFEST.MF and pom-tycho.xml! E.g. after running a Maven release, you need to adapt the version numbers in META-INF/MANIFEST.MF and pom-tycho.xml so that they match the automatically changed version strings in pom.xml.
Also note that you cannot use a Bundle Activator in this project. If you need one, just create another Eclipse-RCP Plug-in that depends on your library and contains a Bundle Activator.

Troubleshooting: After importing as Maven project or converting to Maven project, you might run into build-path problems. Try to resolve using ‘right-click on project -> Maven -> Update Project..’ And/ or fix the Java Build Path of the project (Standard Eclipse source folder is src, in Maven it is src/main/java). java-build-path

change file encoding recursively

Good: Bad: The second one will only work if find will not descend into sub directories, since the directory tree inside of converted will not be created on demand, as it would be the case e.g. for mkdir -p. The result will be a ‘file or directory not found’ error.

OrientDB Document API best practices

OrientDB is an open source NoSQL Database Managment System, which supports multiple models of operation:
  1. Document Model,
  2. Graph Model
  3. and
  4. Object Model.
We will write here about the Document Model. It is the most low-level of the three models and and can provide great performance if no graph or object model is needed. Nevertheless, using a document-based database requires different programming patterns than the ones you know already from the world of object-oriented programming. We will try to compose some best-practices and programming patterns here to guide you when working with document-based database systems in general and the usage of the OrientDB Document API in particular.

  1. Performance and Threading
    ODocument is not thread save. It is not intended to be accessed by multiple threads at the same time. Accessing the db using multiple instances of ODatabaseDocumentTx nevertheless is thread save.

    Anti-Pattern

    Pattern
    Explanations
    Note that documentDatabase itself is not used. ODocument accesses the document database via a thread-local, static instance of ODatabaseDocumentInternal. Consistently, it is required to set this thread-local instance in each thread that is running on the ODocument class.
  2. To be continued

trigger dependency injection from any class

If you need an injectable service, such as the IEventBroker, in a class that is not subject to DI per default, you can trigger DI manually:

Eclipse RCP e4 and JavaFX: minimal working example

  1. Install Java 8

  2. Install JavaFX Tooling and Runtime for Eclipse and OSGi

  3. Make sure Java8 is selected as the default JRE. installed_jres

  4. Create a new Eclipse 4 application project. new_e4_app_project

  5. Make sure you select Create sample content (parts, menu etc.). e4_sample_content

  6. The e4 application. e4-javafx
  7. Create a new class, FXMLController, with the following content: fxmlcontroller
  8. Create a new file, fxml/FXML.fxml, with the following content: fxml
  9. Create a new file, styles/fxml.css, with the following content: fxml-css
  10. Open SamplePart.java and replace its content with the following:
  11. Open META-INF/MANIFEST.MF and add org.eclipse.fx.javafx and org.eclipse.fx.osgi.util to Required Plug-ins. javafx_plugin_requirement
    e4fx_manifest.mf
  12. Open e4-javafx.product and add the following to the VM Arguments:
    -Dosgi.framework.extensions=org.eclipse.fx.osgi
    
    vm_launching
  13. Launch the application: launch_e4_fx_product
    running2

Checked vs. Unchecked Exceptions

Use checked exceptions for recoverable conditions and runtime exceptions for programming errors.

Joshua Bloch, Effective Java
This might be a little abstract, so we to concretize to
User’s error vs. Programmer’s error.

RuntimeException and all its child classes indicate a programmer’s error, while Exception and all its child classes indicate a user’s error:

  1. FileNotFoundException: Can happen anytime, its not a programmer’s mistake but a user’s error (choose a wrong file, working with a read-only or full file system, etc.)
  2. SQLException: Something went wrong with the database operation. Try to recover or notify the user. This example fits very good to the above mentioned ‘recoverable’ condition but can be as well caused by a user’s error, e.g. an invalid search query.
  3. NullPointerException: Clearly a programmer’s error, don’t catch it but make it not happen.
  4. ArithmeticException: Happens for example when you divide by zero. A programmer’s error. Don’t divide by zero.
  5. UnsupportedOperationException: Thrown for example by immutable implementations of Collection (e.g. Collections.unmodifiableList()). It’s the responsibility of the programmer to handle this case.