java - PropertyChangeListener usage check -


good evening so,

i wrote 2 pretty simple classes 1 of projects. first time met such problem, ask if managed tackle problem right way / implementation.

the background quite simple: have channel might either busy or not. if channel busy, means taken servicerequest. once request processed channel should open again.

i googled little bit , found ideas use propertychangedlistener. code down below. please give me comments can regarding code quality / problem solving here. thank you!

test:

@unroll def "when request processed , finished channel free again"() {     given:     def channel = new channel()     def request = new servicerequest()     request.addpropertychangelistener(channel)     when:     channel.setrequest(request)     request.finish();     then:     assert !channel.isbusy() && channel.request == null } 

channel class:

public class channel implements propertychangelistener{  private boolean busy; private servicerequest request;  public channel() {     this.busy = false; }  public boolean isbusy() {     return busy; }  public servicerequest getrequest() {     return request; }  public void setrequest(final servicerequest request) {     this.request = request;     busy = true; }  @override public void propertychange(propertychangeevent evt) {     request = null;     busy = false; } 

servicerequest class:

public class servicerequest {  propertychangesupport support = new propertychangesupport(this);  private string id;  public servicerequest() {     id = "randomlygeneratedid"; }  void addpropertychangelistener(final propertychangelistener l) {     support.addpropertychangelistener(l); }  public void finish() {     id = "";     support.firepropertychange("id", id, "");     support.firepropertychange("request", null, null);     support.firepropertychange("busy", null, false); }  public string getid() {     return id; } 


Comments