Simple bound properties with lambda expressions

As I am climbing the learning curve, I find myself to write code such as

      more.disableProperty().bind(model.temperatureProperty()
         .greaterThanOrEqualTo(100));
      gauge.widthProperty().bind(model.temperatureProperty()
         .multiply(4));

I can see why he must accumulate these trees of expression with methods such as greaterThanOrEqualTo and multiply , but it seems a bit tedious. So, I thought: why not let the lambdas at work? Feed properties and a lambda that says how they should be evaluated when they change. Like this:

      more.disableProperty().bind(observe(model.temperatureProperty(),
            t -> t >= 100));
      gauge.widthProperty().bind(observe(model.temperatureProperty(),
            t -> 4 * t));

It's maybe just me, but I find it easier on the eyes. The implementation of observe is trivial:

   static class Binding1<T, R> extends ObservableValueBase {
      private ObservableValue<T> obs;
      private Function<T, R> fun;
      
      public Binding1(ObservableValue<T> obs, Function<T, R> fun) {
         this.obs = obs;
         this.fun = fun;
         obs.addListener(o -> fireValueChangedEvent());         
      }

      public R getValue() {
         return fun.apply(obs.getValue());
      }
   }

   public static <T, R> ObservableValue<R> observe(ObservableValue<T> obs, Function<T, R> fun) {
      return new Binding1<T, R>(obs, fun);
   }

We could use a couple more of these functions to handle the lambdas with 2 and maybe 3 to 4 parameters.

With this approach, there is no real need to use such things as DoubleProperty because you can do assessments in the lambda and don't need a bunch of methods to replicate the arithmetic operations.

Someone has already thought along those lines, or am I innovate here? Or I forgot something that would condemn this approach?

The class of links has a bunch of useful methods to create links, including createObjectBinding (...), which I think is more or less what you are looking for here:

import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;

public class BindingsWithLambdaTest {

  public static void main(String[] args) {
    final DoubleProperty temp = new SimpleDoubleProperty();
    final BooleanProperty more = new SimpleBooleanProperty();
    final DoubleProperty gauge = new SimpleDoubleProperty();

    more.bind(Bindings.createObjectBinding(
        () -> temp.doubleValue() >= 100,
        temp)
    );
    gauge.bind(Bindings.createObjectBinding(
        () -> 4*temp.doubleValue(),
        temp)
    ); 

    more.addListener(
        (observable, oldValue, newValue) -> System.out.printf("more changed from %s to %s%n", oldValue, newValue)
    );
    gauge.addListener(
        (observable, oldValue, newValue) -> System.out.printf("gauge changed from %.1f to %.1f%n", oldValue.doubleValue(), newValue.doubleValue())
    );

    temp.set(10);
    temp.set(40);
    temp.set(110);
  }

}

Post edited by: James_D (additional code example)

Tags: Java

Similar Questions

  • access to electronic mail with outlook express och webmail

    I get emails with outlook express on a computer using Windows XP.

    I can't access the same e-mails with webmail on another computer using Windows7.

    I guess that emails may be erased in the server.

    How can I get the same emails on both computers?

    Thanks a lot for your help, Walter Widl

    In OE, go to tools | Accounts | Mail | Properties | Advanced and check the leave a copy of messages on the server.  By default, they are deleted when you check mail.

    Steve

  • Cannot send the PDF with Outlook Express.

    PROBLEM SENDING FILE PDF WITH OUTLOOK EXPRESS.

    Here's what's happening.  I created a PDF file in adobe 4 pages, I can reach the PDF file to a mail sent in Outlook Express, but I see where he treats of it says part 1, part 2, part 3, all the way to part 7, when there are only four pages.  The recipient gets 7 different emails and cannot open an any of them.    I have a yahoo email and I can send the PDF file there.   Just frustrating.  I use Windows XP.  Not familiar with computers but I would like someone to tell me the problem.  It has worked before and I don't know what happened to create the question.  Thank you.

    Tools | Accounts | Mail | Properties | Advanced. Uncheck the box: "Break apart messages...". ».

  • It is beneficial to create new static methods to replace lambda expressions?

    Simon told us to replace the lambda expression by method handles. Is there an advantage to do so, such as better performance? And it means that it is always advantageous to create new static methods instead of write lambda expressions?

    What Simon says - or should have said - is to use a reference method, is not a handle method. (A method handle is a construction of low level used by the invokedynamic bytecode and as such is much closer to the JVM. "It is not a feature of the Java language).

    In all cases, it can often be a good idea to use a method instead of an explicit lambda expression reference. Performance is not a problem. I did a comparative analysis and I was unable to discern the difference.

    First of all, you may already have a method that does what you want, so if you need a lambda that does the same thing, just write a method reference in the existing method.

    But sometimes it makes sense to take a lambda expression and then replace the lambda with a method reference refactor in a named method. There are several reasons for this:

    • Testability. A named method is easier to unit test to a lambda expression incorporated in a more complex construction as a data flow pipeline.
    • Reuse. The same logic can be used in several places, by calls direct or other references of the method.
    • Readability. If a lambda begins to be too big (in particular, several lines), it can disrupt the readability of a data flow pipeline.
    • Debugging. While most debuggers can today point stop and step by step through lambda expressions, control flow can be confusing.

    It is above all a question of degree. I find that about 95% of the lambda expressions that I write, or calculate something trivial, for example

    x -> x + 1
    

    or call a method in a way that cannot be expressed using a method reference, for example

    (a, b) -> a.foo(b.getBar())
    

    Rarely, I'll write an inline lambda, such as multi-instructions

    x -> { foo(x); bar(x); }
    

    Anything more complicated I'll tend to refactor in a separate method and use a method reference.

    Finally, a method reference can often be an instance instead of a static method method. If you're in an instance method and you want to write a reference to a method that is called on the same instance, you can write

    this::someMethod
    

    refer to it.

  • airplay iOS 10 with airport express

    Impossible to find airplay with Airport Express, but works well with apple tv

    some1 help me

    Same problem with my iPhone 5 and IPad Mini after upgrading to IOS 10.

    Firmware Express Airport: 7.6.7

  • How can I send emails with outlook express from firefox

    How can I send emails with outlook express from firefox

    See change the program used to open e-mail links for how to configure Firefox to use Outlook Express.

  • Cannot send emails with Outlook Express

    original title: I AM NOT ABLE to SEND EMAIL WITH OUTLOOK EXPRESS but CAN RECIEVE I AI PARLEES WITH OPTUS AND CHECXKED all THE SETTINGS AND ARE OK

    I CHECKED WITH OPTUS OUR CARRIER AND WE CANNOT SEND EMAILS VIA OUTLOOK EXPRESS, THEY SEEM TO THINK THAT IT MAY BE A PROBLEM WITH OUTLOOK CAN ONLY HELP PLEASE

    I CHECKED WITH OPTUS OUR CARRIER AND WE CANNOT SEND EMAILS VIA OUTLOOK EXPRESS, THEY SEEM TO THINK THAT IT MAY BE A PROBLEM WITH OUTLOOK CAN ONLY HELP PLEASE

  • Cannot send to multiple addresses with outlook express

    Cannot send to multiple addresses with outlook express

    Hello
    even if you weren't too fourth to come up with more details on this issue, guide for the question on Outlook express here.
    If you have more specific questions, please include more details.
    http://support.Microsoft.com/kb/835830
    B Eddie

  • have dell dimension 4700 xp need help with outlook express I have error message and can not send email

    I need help to figure out why my outlook express does not work when I try to send a link, or email. I have a dell dimension 4700 xp restored. The error message cannot find the server with an error code number. I am a newbee so one who responds to it will have to break it down for me. So far, I had lots of help from proceeding to audio and graphics, and I am totally grateful for the help I get.

    Thank you, Gina Davis

    I have

    He has already worked with Outlook Express? Using OE? Hotmail is a Webmail service.

    How to add your Hotmail e-mail account to Microsoft Outlook Express
    http://windowslivehelp.com/solution.aspx?SolutionID=99d4b13d-13dB-40D8-9cdf-172002d4194c

    If you don't want to use OE to access Hotmail and want to just use IE to access Hotmail, then just copy the link and paste it into a new message from Hotmail.

  • Unable to connect to pop3 email such as Hotmail or Gmail with Outlook Express or MS Outlook.

    Original title: network

    Dear Sir, that's what I want to know that I'm using windows xp sp3 and using the internet connection through the proxy server. I have 2 problems

    1 > connection pop3 email such as hotmail or gmail with outlook express or outlook ms but our local pop3 service working which is connection IP impossible.
    2 > cannot open the webcam on yahoo Messenger. display 'waiting for reconnect' and then see the "server error" the but video calls on Skype.
    Help, please
    Thank you debabrata

    Hello

    (1) is your computer connected to a domain?

    (2) what exactly happens when you connect to outlook express?

    If you have problems with Microsoft Outlook, then I suggest you to send your query in the Sub forum.

    Support for Microsoft office outlook: http://answers.microsoft.com/en-us/office/forum/outlook

    Yahoo messenger is also a non-Microsoft program, so I suggest you get in touch with the respective support.

    Support for Yahoo: http://help.yahoo.com/kb/index;_ylt=AmiObOVCcmDR9PqYH9pefFm5OSV4?locale=en_US&page=product&y=PROD_MSNG

    Note: This forum is supported only for the Microsoft windows desktop.

    Hope this information is useful.

  • How can I synchronize my folder of Yahoo! Mail Plus sent with Outlook Express?

    How can I synchronize my folder of Yahoo! Mail Plus sent with Outlook Express?

    I'm not sure you can.  Apparently, you can have a POP3 and IMAP account need you, but you can ask here: http://answers.yahoo.com/dir/index?link=over&sid=396546091

    Steve

  • How to block the recipients to see the list of multiple recipients on the email sent with outlook express 6.0?

    How can I block message recipients to see the list of multiple recipients on the email sent with outlook express 6.0?

    Put the recipients in the BCC (blind carbon copy) field instead of the Cc field. If you don't see the BCC field and then in your new message go to view > all headers.

    DavidF

  • Send and receive mail with Outlook Express.

    I can receive email with outlook express, but can't open it. The message body is empty. I can't type in the area of the body to create messages. When you try to send a message I get this error message. "message cannot be sent because it has not been downloaded. It DOWNLOADS EVER! I tried to send many messages to test and get the same message. My cable company is puzzled also. I need help!

    Hi Gary,.

    Take a look at the following link and see if the steps it help:

    Let me know if you need help!

    Cody C
    Microsoft Answers Support Engineer
    Visit our Microsoft answers feedback Forum and let us know what you think.

  • Smartphones blackBerry sync with outlook express problem

    I seem to get my "BOLD" to synchronize perfectly, everything except my contacts. I can sync with Yahoo, but when I want to synchronize with Outlook Express (the one I want to use), I get an error message. When I try to sync DM compares my contacts to Outlook contacts and tells my there are two changes to be made. I agree with the changes, and then this error message is displayed "connector specific error". This happens when I synchronize using USB or Bluetooth. Please help me!

    Note the two contacts, and delete them entirely, then sync, then enter again. Paper rocks.

  • Lesson 1 - Lambda expression syntax

    Hello:

    I'm trying to code a Hello everyone using lambda expression but I get an error, this is the code:

    public class {Lesson1

    /**

    @param args command-line arguments

    */

    Public Shared Sub main (String [] args) {}

    Call();

    }

    private public static call() Sub {}

    ()-> System.out.println ("Hello world!");

    }

    }

    I get a compilation error not a statement

    and of course, if I run it, I get: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - not a statement

    I use the syntax that is in course-1 - 2.pdf, page 3

    What I am doing wrong?

    Lambda expressions are meant to be used somewhere and are not allowed to be right on their own line/statement. It's hard to explain, since known functional interfaces that is in a later lesson.

    Fortunately, the functional interface in your example is passable. Is not new in Java 8. Here is a concrete example of what you're trying to do:

        publicstaticvoid main(String[] args) {
            call(() -> System.out.println("Hello world!"));
        }
    
        private static void call(Runnable r) {
          r.run();
        }
    

Maybe you are looking for

  • When Firefox has been updated automatically, it deleted my yahoo toolbar and I cannot reinstall it.

    When Firefox is automatically updated to 29, he deleted my yahoo toolbar and I cannot reinstall it. I go through the motions of the installation of the toolbar, and when I restart Firefox, the toolbar is there for a split second and then is removed f

  • Can I protect as installed extensions?

    Can I protect as installed extensions? I have an extension such as parental control, but I can't use this extension if the user can simply disable or remove in order to protect the not deleted? a option to hide the menu extensions or passwords? I don

  • How to activate the query suggestions

    How the query suggestions can be enabled in the Firefox address bar?

  • PXI-6713

    Hello world I have a data acquisition called NI PXI-6713. Can someone tell me if it is possible to use this card to acquire a voltage signal? This card can support analog inputs? Or I'll buy some new maps to measure the signals and do some math FFT o

  • Windows Live Mail Emails appear when searching...

    Hi guys,. How to hide my emails in windows live mail appear on searches? They appear when I'm looking for animals in the start menu. Brian,