Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement the 'SequenceEqual' operator #575

Merged
merged 2 commits into from
Dec 8, 2013

Conversation

zsxwing
Copy link
Member

@zsxwing zsxwing commented Dec 6, 2013

Hi, this PR reimplemented the SequenceEqual operator #76 and should fix the issue #564.

The wiki page https://github.com/Netflix/RxJava/wiki/Observable-Utility-Operators#sequenceequal still needs to be updated. @DavidMGross could you help update the marble diagram of SequenceEqual? Thanks!

@cloudbees-pull-request-builder

RxJava-pull-requests #512 SUCCESS
This pull request looks good

@DavidMGross
Copy link
Collaborator

Okay; I think I've got it corrected. It only covers one condition, so might
benefit from having some more examples, but it'll do for now.

On Fri, Dec 6, 2013 at 7:13 AM, CloudBees pull request builder plugin <
notifications@github.com> wrote:

RxJava-pull-requests #512https://netflixoss.ci.cloudbees.com/job/RxJava-pull-requests/512/SUCCESS
This pull request looks good


Reply to this email directly or view it on GitHubhttps://github.com//pull/575#issuecomment-30001152
.

David M. Gross
PLP Consulting

@zsxwing
Copy link
Member Author

zsxwing commented Dec 7, 2013

@DavidMGross Looks cool. Thanks.

@cloudbees-pull-request-builder

RxJava-pull-requests #514 SUCCESS
This pull request looks good

@akarnokd
Copy link
Member

akarnokd commented Dec 7, 2013

Good job, just a few things.

  • You are using gate to ensure concurrency, and you access the firstDone and secondDone while synchronizing over gate. I believe these two can be simple boolean fields instead of AtomicBooleans.
  • You are setting the firstValues and secondValues in the init method into a volatile fields. I believe these can be moved into the constructor and set to a regular private final List type and accessor.
  • [Optional] you might want to call clear on both firstValues and secondValues if there was a termination condition so they don't hold onto queued values until the client observer disappears.

@samuelgruetter
Copy link
Contributor

I really think that sequenceEqual should be implemented in terms of other operators. Don't reinvent the wheel with every operator you add ;-)
In Scala, I'd do it like this:

def sequenceEqual[T](o1: Observable[T], o2: Observable[T]): Observable[Boolean] = {
  val obs1: Observable[Option[T]] = o1.map(Some(_)) ++ Observable(None) // ++ is concat
  val obs2: Observable[Option[T]] = o2.map(Some(_)) ++ Observable(None)
  // If different length, comparing the `None` of the shorter with `Some(element)` 
  // of the longer will return false.
  (obs1 zip obs2).forall(pair => pair._1 == pair._2)
}

@zsxwing
Copy link
Member Author

zsxwing commented Dec 7, 2013

@samuelgruetter , great idea. Really thanks. I overrode the previous commit. As their is no Option in Java, I use Notification instead.

@cloudbees-pull-request-builder

RxJava-pull-requests #515 SUCCESS
This pull request looks good

@zsxwing
Copy link
Member Author

zsxwing commented Dec 7, 2013

I also have a question about Notification.hasValue.

    /**
     * Retrieves a value indicating whether this notification has a value.
     * 
     * @return a value indicating whether this notification has a value.
     */
    public boolean hasValue() {
        return isOnNext() && value != null;
    }

Here it checks if value is not null. But I think value can be null.

I also checked the Rx.Net codes here: http://rx.codeplex.com/SourceControl/latest#Rx.NET/Source/System.Reactive.Core/Reactive/Notification.cs

            /// <summary>
            /// Returns true.
            /// </summary>
            public override bool HasValue { get { return true; } }

It returns true directly in OnNextNotification.

If Notification.hasValue has the same meaning in RxJava and Rx.Net. I think && value != null needs to be removed.

@cloudbees-pull-request-builder

RxJava-pull-requests #516 SUCCESS
This pull request looks good

@samuelgruetter
Copy link
Contributor

Much nicer now ;-)

Regarding hasValue: I don't know. What is it good for if it's the same as isOnNext? And why would one need the current version? Imho it could just be removed.

Something else regarding null values and sequenceEqual with default equality: I think you shouldn't call onError(NullPointerException), but just onNext(true) or onNext(false).

@zsxwing
Copy link
Member Author

zsxwing commented Dec 7, 2013

@samuelgruetter Thanks for reminding me the null issue.

@cloudbees-pull-request-builder

RxJava-pull-requests #517 SUCCESS
This pull request looks good

benjchristensen added a commit that referenced this pull request Dec 8, 2013
Reimplement the 'SequenceEqual' operator
@benjchristensen benjchristensen merged commit c44d7cb into ReactiveX:master Dec 8, 2013
@zsxwing zsxwing deleted the sequence-equal branch December 9, 2013 01:42
rickbw pushed a commit to rickbw/RxJava that referenced this pull request Jan 9, 2014
Reimplement the 'SequenceEqual' operator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants