Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Commit

Permalink
Merge pull request #86 from mr-max/patch-1
Browse files Browse the repository at this point in the history
Allow getting MenuItems as Sequence
  • Loading branch information
yanex committed Sep 22, 2015
2 parents cc65a21 + cb1a68c commit 0b76ef9
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions dsl/static/src/common/menuItemsSequences.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

@file:JvmMultifileClass
@file:JvmName("MenuItemsSequencesKt")
package org.jetbrains.anko

import android.view.Menu
import android.view.MenuItem
import java.util.*

fun Menu.itemsSequence(): Sequence<MenuItem> = MenuItemsSequence(this)

private class MenuItemsSequence(private val menu: Menu) : Sequence<MenuItem> {
override fun iterator(): Iterator<MenuItem> {
return MenuItemIterator(menu)
}

private class MenuItemIterator(private val menu: Menu) : Iterator<MenuItem> {
private var index = 0
private val count = menu.size()

override fun next(): MenuItem {
if (!hasNext()) {
throw NoSuchElementException()
}

return menu.getItem(index++)
}

override fun hasNext(): Boolean {
if (count != menu.size()) {
throw ConcurrentModificationException()
}

return index < count
}
}
}

0 comments on commit 0b76ef9

Please sign in to comment.