text:
import { view as itemView } from '../views/item-view';
import { view as newsView } from '../views/news-view';
import { view as userView } from '../views/user-view';
import { State } from '../types/state';

export type Helpers = any;
export type View = any;

type ViewState = {
  currentPage: string;
  state: State;
};

const viewState = (state: State, _: Helpers): ViewState => {
  if (!state) return null;
  return {
    currentPage: state.currentPage,
    state
  };
};

const headerView = (_: ViewState, helpers: Helpers): View => {
  const { create: h } = helpers;
  return h('div#header', [
    h('a#yc', { href: 'http://www.ycombinator.com' }, [
      h('img', { src: 'https://news.ycombinator.com/y18.gif' })
    ]),
    ' ',
    h('h1', [
      h('a', { href: '#/' }, ['Hacker News'])
    ]),
    h('span.source', [
      'Built with ',
      h('a', { href: 'https://github.com/bouzuya/b-o-a' }, [
        'b-o-a'
      ]),
      ' | ',
      h('a', {
        href: 'https://github.com/boajs/hackernews-boa',
        target: '_blank'
      }, [
          'Source'
        ])
    ])
  ]);
};

const mainView = (state: ViewState, helpers: Helpers): View => {
  const views: { [page: string]: (state: State, helpers: Helpers) => View; } = {
    item: itemView,
    news: newsView,
    user: userView
  };
  const view = views[state.currentPage];
  if (!view) return null;
  return view(state.state, helpers);
};

const render = (state: ViewState, helpers: Helpers): View => {
  if (!state) return null;
  const { create: h } = helpers;
  return h('div#app', [
    h('div#wrapper', [
      headerView(state, helpers),
      mainView(state, helpers)
    ])
  ]);
};

const view: (state: State, helpers: Helpers) => View = (state, helpers) =>
  render(viewState(state, helpers), helpers);

export { view };


text:
module SpecSupport
  module DbHelper
    def table_counts
      @conn = ActiveRecord::Base.connection
      tables = @conn.tables
      tables.each do |t|
        result = @conn.execute("select count(*) from #{t}")
        rec = result.first
        puts "#{t}: #{rec['count']}"
      end
    end
  end
end

redpajama_set_name: RedPajamaGithub


text:
#import <UIKit/UIKit.h>
#import "alljoyn/about/AJNAnnouncement.h"

@interface AnnounceTextViewController : UIViewController
@property (strong, nonatomic) AJNAnnouncement *ajnAnnouncement;
@end


text:
module Reverse where

rvrs :: String -> String
rvrs x = drop 9 x ++ take 4 (drop 5 x) ++ take 5 x

main :: IO ()
main = print $ rvrs "Curry is awesome"


text:
<?php

if (!defined('BB_ROOT')) {
    die(basename(__FILE__));
}

require(CLASS_DIR . 'sitemap.php');

$map = new sitemap();
$map->create();

if (file_exists(BB_ROOT . "/sitemap/sitemap.xml")) {
    $map_link = make_url('/sitemap/sitemap.xml');

    $map->send_url("http://google.com/webmasters/sitemaps/ping?sitemap=", $map_link);
    $map->send_url("http://ping.blogs.yandex.ru/ping?sitemap=", $map_link);
    $map->send_url("http://www.bing.com/ping?sitemap=", $map_link);
    $map->send_url("http://rpc.weblogs.com/pingSiteForm?name=InfraBlog&url=", $map_link);
}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ViewModelPerTab
{
    /// <summary>
    /// Interaction logic for Tab2.xaml
    /// </summary>
    public partial class Tab2 : UserControl
    {
        public Tab2()
        {
            InitializeComponent();
        }
    }
}


text:
<?php

namespace Tests\Feature;

use Tests\TestCase;
use Knot\Notifications\PostReactedTo;
use Illuminate\Support\Facades\Notification;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ReactionsTest extends TestCase
{
    use RefreshDatabase;

    protected $user;
    protected $post;

    public function setup()
    {
        parent::setup();

        Notification::fake();
        $this->user = create('Knot\Models\User');
        $this->post = create('Knot\Models\TextPost', ['user_id' => $this->user->id])->post;
        $this->authenticate();
    }

    /** @test */
    public function a_user_cannot_react_to_a_post_that_does_not_belong_to_a_friend()
    {
        $this->withExceptionHandling();

        $this->postJson('api/posts/'.$this->post->id.'/reactions', ['type' => 'smile'])->assertStatus(403);
    }

    /** @test */
    public function a_user_can_react_to_a_post_if_it_does_belong_to_a_friend()
    {
        $this->createFriendship(auth()->user(), $this->user);

        $this->postJson('api/posts/'.$this->post->id.'/reactions', ['type' => 'smile'])->assertStatus(200);
        $this->assertDatabaseHas('reactions', ['post_id' => $this->post->id]);
    }

    /** @test */
    public function a_reaction_type_must_be_one_of_the_set_reactions()
    {
        $this->withExceptionHandling();

        $this->createFriendship(auth()->user(), $this->user);

        $this->postJson('api/posts/'.$this->post->id.'/reactions', ['type' => 'cringe'])->assertStatus(422);
    }

    /** @test */
    public function a_post_author_receives_a_notification_when_someone_reacts_to_their_post()
    {
        $this->createFriendship(auth()->user(), $this->user);
        $this->postJson('api/posts/'.$this->post->id.'/reactions', ['type' => 'smile']);

        Notification::assertSentTo($this->user, PostReactedTo::class);
    }
}


text:
// -----------------------------------------------------------------------
// <copyright file="MessageEnvelope.cs" company="YamNet">
//   Copyright (c) 2013 YamNet contributors
// </copyright>
// -----------------------------------------------------------------------

namespace YamNet.Client
{
    using Newtonsoft.Json;

    /// <summary>
    /// Message API response that contains a list of messages (thread starters),
    /// a list of references, meta object (perspective data), and, optionally,
    /// a collection of messages for each thread.
    /// </summary>
    public sealed class MessageEnvelope
    {
        // TODO: add `external_references` object

        /// <summary>
        /// Gets or sets the threaded messages.
        /// </summary>
        /// <value>The threaded messages.</value>
        [JsonProperty("threaded_extended")]
        public MessageThread MessageThreads { get; set; }

        /// <summary>
        /// Gets or sets the messages.
        /// </summary>
        /// <value>The messages.</value>
        [JsonProperty("messages")]
        public Message[] Messages { get; set; }

        /// <summary>
        /// Gets or sets the references.
        /// </summary>
        /// <value>The references.</value>
        [JsonProperty("references")]
        [JsonConverter(typeof(ReferenceJsonConverter))]
        public IReference[] References { get; set; }

        /// <summary>
        /// Gets or sets the meta.
        /// </summary>
        /// <value>The meta.</value>
        [JsonProperty("meta")]
        public Meta Meta { get; set; }
    }
}


text:
from __future__ import print_function, division, absolute_import

from struct import unpack, unpack_from, calcsize

from ..message import BackendMessage
from ....datatypes import getTypeName

class ParameterDescription(BackendMessage):
    message_id = b't'

    def __init__(self, data):
        BackendMessage.__init__(self)
        self.parameters = []
        self.parameter_count = unpack('!H', data[0:2])[0]
        if self.parameter_count == 0:
            return

        # read type pool
        # used for special types e.g. GEOMETRY, GEOGRAPHY
        user_types = []
        type_pool_count = unpack('!I', data[2:6])[0]
        pos = 6
        for _ in range(type_pool_count):
            base_type_oid = unpack('!I', data[pos:(pos + 4)])[0]
            pos += 4
            type_name = unpack_from("!{0}sx".format(data.find(b'\x00', pos) - pos), data, pos)[0]
            pos += len(type_name) + 1
            user_types.append((base_type_oid, type_name))

        # read info of each parameter
        offset = calcsize("!BIiH")
        for _ in range(self.parameter_count):
            field_info = unpack_from("!BIiH", data, pos)
            pos += offset

            if field_info[0] == 1:
                data_type_oid, data_type_name = user_types[field_info[1]]
            else:
                data_type_oid = field_info[1]
                data_type_name = getTypeName(data_type_oid, field_info[2])

            self.parameters.append({
                'data_type_oid': data_type_oid,
                'data_type_name': data_type_name,
                'type_modifier': field_info[2],
                'null_ok': field_info[3] != 1,
            })

    def __str__(self):
        return "ParameterDescription: {}".format(self.parameters)

BackendMessage.register(ParameterDescription)


text:
@implementation NetworkManager

- (void)loginUserWithEmail:(NSString *)email password:(NSString *)password success:(SuccessResponse)success failure:(FailureResponse)failure {
    NSLog(@"%s", sel_getName(_cmd));
    [self asyncRequestWithSuccess:success failure:failure response:@{@"user" : @"user"}];
}

- (void)registerUserWithEmail:(NSString *)email password:(NSString *)password firstName:(NSString *)firstName lastName:(NSString *)lastName success:(SuccessResponse)success failure:(FailureResponse)failure {
    [self asyncRequestWithSuccess:success failure:failure response:@{@"user" : @"user"}];
}

- (void)verifyEmail:(NSString *)email success:(SuccessResponse)success failure:(FailureResponse)failure {
    [self asyncRequestWithSuccess:success failure:failure response:@{@"email" : @"email"}];
}

- (void)logoutUserWithSuccess:(SuccessResponse)success failure:(FailureResponse)failure {
    NSLog(@"%s", sel_getName(_cmd));
    [self asyncRequestWithSuccess:success failure:failure response:@{@"success" : @"success"}];
}

- (void)getUserInfoWithSuccess:(SuccessResponse)success failure:(FailureResponse)failure {
    NSLog(@"%s", sel_getName(_cmd));
    [self asyncRequestWithSuccess:success failure:failure response:@{@"userinfo" : @"userinfo"}];
}

- (void)asyncRequestWithSuccess:(SuccessResponse)success failure:(FailureResponse)failure response:(NSDictionary *)response {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
        if (arc4random() % 100 != 0) {
            if (success) {
                success(response);
            }
        } else {
            if (failure) {
                failure(nil);
            }
        }
    });
}

@end

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wincomplete-implementation"

@implementation NetworkManager (RAC)

- (void)forwardInvocation:(NSInvocation *)reactiveMethodInvocation {

    NSString *reactiveSelectorName = NSStringFromSelector([reactiveMethodInvocation selector]);
    SEL originalSelector = [self findOriginalSelectorForRACSelector:[reactiveMethodInvocation selector]];

    NSArray *paramsArray = [reactiveSelectorName componentsSeparatedByString:@":"];
    int paramsCount = [paramsArray count] - 1;

    NSInvocation *originalMethodInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:originalSelector]];
    originalMethodInvocation.target = self;
    originalMethodInvocation.selector = originalSelector;

    // Passing all params to the original method invocation
    int paramsOffset = 2;
    for (int i = 0; i < paramsCount; ++i) {
        id arg;
        [reactiveMethodInvocation getArgument:&arg atIndex:i + paramsOffset];
        [originalMethodInvocation setArgument:&arg atIndex:i + paramsOffset];
    }

    int successCallbackParamIndex = paramsOffset + paramsCount;
    int errorCallbackParamIndex = paramsOffset + paramsCount + 1;

    RACSignal *signal = [[RACSignal createSignal:^RACDisposable *(id <RACSubscriber> subscriber) {
        SuccessResponse successBlock = ^(id res) {
            [subscriber sendNext:res];
            [subscriber sendCompleted];
        };
        FailureResponse failureResponse = ^(NSError *error) {
            [subscriber sendError:error];
        };
        [originalMethodInvocation setArgument:&successBlock atIndex:successCallbackParamIndex];
        [originalMethodInvocation setArgument:&failureResponse atIndex:errorCallbackParamIndex];

        [originalMethodInvocation invoke];

//        id<Cancelable> cancelable;
//        [originalMethodInvocation getReturnValue:&cancelable];
//
//        return [RACDisposable disposableWithBlock:^{
//            [cancelable cancel];
//        }];
        return nil;

    }] setNameWithFormat:@"RACified %@", NSStringFromSelector(originalSelector)];

    [reactiveMethodInvocation setReturnValue:&signal];

}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {

    NSMethodSignature *superResult = [super methodSignatureForSelector:aSelector];
    if (superResult) {
        return superResult;
    }

    SEL originalSelectorForRACSelector = [self findOriginalSelectorForRACSelector:aSelector];
    if (!originalSelectorForRACSelector) {
        return nil;
    }

    NSArray *paramsArray = [NSStringFromSelector(originalSelectorForRACSelector) componentsSeparatedByString:@":"];
    int paramsCount = [paramsArray count] - 1;
    if (paramsCount == 0) {
        return [NSMethodSignature signatureWithObjCTypes:"@@:"];
    }

    NSMutableString *obcjTypesString = [NSMutableString string];
    [obcjTypesString appendString:@"@@"];
    for (int i = 0; i < paramsCount; ++i) {
        [obcjTypesString appendString:@":@"];
    }
    return [NSMethodSignature signatureWithObjCTypes:[obcjTypesString cStringUsingEncoding:NSUTF8StringEncoding]];

}

- (SEL)findOriginalSelectorForRACSelector:(SEL)pSelector {
    NSString *selectorName = NSStringFromSelector(pSelector);

    unsigned int methodCount = 0;
    Method *methods = class_copyMethodList([self class], &methodCount);
    @try {
        for (unsigned int i = 0; i < methodCount; i++) {
            Method method = methods[i];
            SEL aSelector = method_getName(method);
            NSString *existingSelector = NSStringFromSelector(aSelector);
            if ([existingSelector hasPrefix:selectorName]) {
                return aSelector;
            }
        }
    }
    @finally {
        free(methods);
    }
    return nil;
}

@end

#pragma clang diagnostic pop


text:
<?php
namespace Concrete\Controller\Panel\Detail\Theme;

use Concrete\Controller\Backend\UserInterface as BackendInterfaceController;
use Concrete\Core\Http\Request;
use Concrete\Core\Page\Page;
use Concrete\Core\Page\Theme\Theme;
use Concrete\Core\Page\View\Preview\ThemeCustomizerRequest;
use Concrete\Core\Permission\Checker;
use Concrete\Core\Permission\Key\Key as PermissionKey;
use Concrete\Core\StyleCustomizer\Compiler\Compiler;
use Concrete\Core\StyleCustomizer\Normalizer\NormalizedVariableCollectionFactory;
use Concrete\Core\StyleCustomizer\Skin\SkinInterface;
use Concrete\Core\StyleCustomizer\Style\StyleValueListFactory;
use Symfony\Component\HttpFoundation\Response;
use Concrete\Core\Error\UserMessageException;

class PreviewSkin extends BackendInterfaceController
{
    protected $viewPath = '/panels/details/theme/preview_skin';

    public function canAccess()
    {
        $pk = PermissionKey::getByHandle('customize_themes');
        return $pk->validate();
    }

    public function view($pThemeID, $skinIdentifier, $pageID)
    {
        $theme = Theme::getByID($pThemeID);
        $page = Page::getByID($pageID);
        $this->set('previewPage', $page);
        $this->set('pThemeID', $pThemeID);
        $this->set('skinIdentifier', $skinIdentifier);
        $this->set('skin' , $theme->getSkinByIdentifier($skinIdentifier));

        $activeSkin = SkinInterface::SKIN_DEFAULT;
        $site = $page->getSite();
        if ($site) {
            if ($site->getThemeSkinIdentifier()) {
                $activeSkin = $site->getThemeSkinIdentifier();
            }
        }
        $this->set('token', $this->app->make('token'));
        $this->set('activeSkin', $activeSkin);
    }

    public function viewIframe($pThemeID, $skinIdentifier, $pageID)
    {
        if ($this->app->make('token')->validate()) {
            $page = Page::getByID($pageID);
            $checker = new Checker($page);
            if ($checker->canViewPage()) {

                $theme = Theme::getByID($pThemeID);
                $customizer = $theme->getThemeCustomizer();
                $skin = $theme->getSkinByIdentifier($skinIdentifier);

                $req = Request::getInstance();
                $req->setCurrentPage($page);
                $controller = $page->getPageController();
                $controller->disableEditing();
                $view = $controller->getViewObject();
                $previewRequest = new ThemeCustomizerRequest();

                $compiler = $this->app->make(Compiler::class);
                $styleValueListFactory = $this->app->make(StyleValueListFactory::class);
                $variableCollectionFactory = $this->app->make(NormalizedVariableCollectionFactory::class);

                if ($this->request->request->has('styles')) {
                    // This is a preview request with custom, changed style data. Let's parse
                    // that data and compile it into a temporary CSS file.
                    $styles = json_decode($this->request->request->get('styles'), true);
                    $preset = $customizer->getPresetByIdentifier($skin->getPresetStartingPoint());
                    $styleValueList = $styleValueListFactory->createFromRequestArray($customizer->getThemeCustomizableStyleList($preset), $styles);
                    $collection = $variableCollectionFactory->createFromStyleValueList($styleValueList);
                    $result = $compiler->compileFromPreset($customizer, $preset, $collection);

                    if ($this->request->request->has('customCss')) {
                        // This is custom CSS from the "Custom CSS" button at the bottom of the customizer. This just gets
                        // appended to the SCSS generated from the theme.
                        $result .= $this->request->request->get('customCss');
                    }

                } else {
                    // Let's turn out selected preset into CSS on the fly.
                    $collection = $variableCollectionFactory->createFromCustomSkin($skin);
                    $result = $compiler->compileFromCustomSkin($customizer, $skin, $collection);
                }

                $previewRequest->setCustomCss($result);
                $view->setCustomPreviewRequest($previewRequest);

                $response = new Response();
                $content = $view->render();
                $response->setContent($content);

                return $response;

            }
        }
        throw new UserMessageException(t('Access Denied'));
    }

}


text:
module Azure::ServiceFabric::V6_2_0_9
  module Models
    #
    # Defines values for NodeDeactivationTaskType
    #
    module NodeDeactivationTaskType
      Invalid = "Invalid"
      Infrastructure = "Infrastructure"
      Repair = "Repair"
      Client = "Client"
    end
  end
end


text:
package org.apache.giraph.comm.aggregators;

import com.google.common.collect.Maps;

import java.util.Map;

/**
 * Cache which counts the number of flushes per task id (destination worker
 * id), so we know how many requests were sent to the worker
 */
public abstract class CountingCache {
  /** Counts the number of flushes for each worker */
  private Map<Integer, Long> countMap = Maps.newHashMap();

  /**
   * Increase count of flushes for worker with desired task id. Subclasses
   * should call this method whenever flush is called.
   *
   * @param taskId Task id of worker
   */
  protected void incrementCounter(Integer taskId) {
    Long currentCount = countMap.get(taskId);
    countMap.put(taskId,
        (currentCount == null) ? 1 : (currentCount + 1));
  }

  /**
   * Get number of flushes for worker with desired task id
   *
   * @param taskId Task id of worker
   * @return Number of objects for the worker
   */
  protected long getCount(Integer taskId) {
    Long count = countMap.get(taskId);
    if (count == null) {
      return 0;
    } else {
      return count.longValue();
    }
  }

  /**
   * Reset the counts
   */
  public void reset() {
    countMap.clear();
  }
}


text:
#include <avr/io.h>
#include <avr/interrupt.h>

#include "usiTwiSlave.h"
#include "mod_led.h"
#include "mod_adc.h"

#define SLAVE_ADRS	0x40

int main(void)
{
	uint8_t data;

	mod_led_init();
	usiTwiSlaveInit( SLAVE_ADRS );	// Initialize USI hardware for I2C Slave operation.
	mod_adc_init();

	sei();							// Enable interrupts.

	usitwiSlaveEnable();			// Enable the USI interface to receive data.

	// A simple loop to check for I2C Commands.
	// A state variable is needed to process multi-byte messages.
	// 01, 05 are Writes. 04 is a Read.
	while(1)
	{
		if( usiTwiDataInReceiveBuffer() )
		{
			data = usiTwiReceiveByte();
			// Change LED state
			// If the data is 00, then turn OFF the LED. Turn it ON for any non-zero value.
			// NOTE: LED hardware is wired 'Active LOW'.
			if( data == 0)
			{
				mod_led_off();				// Turn LED OFF.
			}
			else
			{
				mod_led_on();				// Turn LED ON.
			}
		}
	}
}


text:
.aui-colorpickergrid {
	width: 340px;
}
.aui-colorpickergrid .aui-colorpicker-container {
	height: auto;
	line-height: 0;
	text-align: center;
}

.aui-colorpickergrid-item {
	cursor: pointer;
	display: inline-block;
	height: 15px;
	margin: 3px;
	padding: 2px;
	width: 15px;
}

.aui-colorpickergrid-item-content {
	display: block;
	height: 100%;
}


text:
class Backend::ShipmentsController < Backend::BaseController
  before_action :set_shipment, only: [:show, :edit, :update, :destroy]

  # GET /shipments
  # GET /shipments.json
  def index
    @shipments = Shipment.page params[:page]
  end

  # GET /shipments/1
  # GET /shipments/1.json
  def show
  end

  # GET /shipments/new
  def new
    @shipment = Shipment.new
  end

  # GET /shipments/1/edit
  def edit
  end

  # POST /shipments
  # POST /shipments.json
  def create
    @shipment = Shipment.new(shipment_params)

    respond_to do |format|
      if @shipment.save
        format.html { redirect_to [:admin, @shipment], notice: 'Shipment was successfully created.' }
        format.json { render action: 'show', status: :created, location: @shipment }
      else
        format.html { render action: 'new' }
        format.json { render json: @shipment.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /shipments/1
  # PATCH/PUT /shipments/1.json
  def update
    respond_to do |format|
      if @shipment.update(shipment_params)
        format.html { redirect_to [:admin, @shipment], notice: 'Shipment was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @shipment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /shipments/1
  # DELETE /shipments/1.json
  def destroy
    @shipment.destroy
    respond_to do |format|
      format.html { redirect_to admin_shipments_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_shipment
      @shipment = Shipment.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def shipment_params
      params.require(:shipment).permit(:tracking, :number, :cost, :shipped_at, :order_id, :address_id, :state)
    end
end


text:
package com.facebook.buck.json;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.facebook.buck.log.Logger;
import com.facebook.buck.rules.BuckPyFunction;
import com.facebook.buck.rules.ConstructorArgMarshaller;
import com.facebook.buck.rules.Description;
import com.facebook.buck.util.Console;
import com.facebook.buck.util.InputStreamConsumer;
import com.facebook.buck.util.ProjectFilesystem;
import com.facebook.buck.util.Threads;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.CharStreams;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

/**
 * Delegates to buck.py for parsing of buck build files.  Constructed on demand for the
 * parsing phase and must be closed afterward to free up resources.
 */
public class ProjectBuildFileParser implements AutoCloseable {

  /** Path to the buck.py script that is used to evaluate a build file. */
  private static final String PATH_TO_BUCK_PY = System.getProperty("buck.path_to_buck_py",
      "src/com/facebook/buck/parser/buck.py");

  private static final Logger LOG = Logger.get(ProjectBuildFileParser.class);

  private final ImmutableMap<String, String> environment;

  private Optional<Path> pathToBuckPy;

  @Nullable private Process buckPyProcess;
  @Nullable BuildFileToJsonParser buckPyStdoutParser;
  @Nullable private BufferedWriter buckPyStdinWriter;

  private final Path projectRoot;
  private final ImmutableSet<Path> ignorePaths;
  private final ImmutableSet<Description<?>> descriptions;
  private final ImmutableList<String> commonIncludes;
  private final String pythonInterpreter;
  private final Console console;

  private boolean isServerMode;

  private boolean isInitialized;
  private boolean isClosed;

  protected ProjectBuildFileParser(
      ProjectFilesystem projectFilesystem,
      Iterable<String> commonIncludes,
      String pythonInterpreter,
      ImmutableSet<Description<?>> descriptions,
      Console console,
      ImmutableMap<String, String> environment) {
    this.projectRoot = projectFilesystem.getRootPath();
    this.descriptions = Preconditions.checkNotNull(descriptions);
    this.ignorePaths = projectFilesystem.getIgnorePaths();
    this.commonIncludes = ImmutableList.copyOf(commonIncludes);
    this.pythonInterpreter = Preconditions.checkNotNull(pythonInterpreter);
    this.pathToBuckPy = Optional.absent();
    this.console = Preconditions.checkNotNull(console);
    this.environment = Preconditions.checkNotNull(environment);

    // Default to server mode unless explicitly unset internally.
    setServerMode(true);
  }

  /**
   * Sets whether buck.py will use --server mode.  Server mode communicates via
   * stdin/stdout to accept new BUCK files to parse in a long running fashion.  It
   * also changes the stdout format so that output has an extra layer of structure
   * sufficient to communicate state and coordinate on individual BUCK files
   * submitted.
   * <p>
   * Note that you must not invoke this method after initialization.
   */
  private void setServerMode(boolean isServerMode) {
    ensureNotClosed();
    ensureNotInitialized();

    this.isServerMode = isServerMode;
  }

  private void ensureNotClosed() {
    Preconditions.checkState(!isClosed);
  }

  private void ensureNotInitialized() {
    Preconditions.checkState(!isInitialized);
  }

  /**
   * Initialization on demand moves around the performance impact of creating the Jython
   * interpreter to when parsing actually begins.  This makes it easier to attribute this time
   * to the actual parse phase.
   */
  @VisibleForTesting
  public void initIfNeeded() throws IOException {
    ensureNotClosed();
    if (!isInitialized) {
      init();
      isInitialized = true;
    }
  }

  /**
   * Initialize the parser, starting buck.py.
   */
  private void init() throws IOException {
    ProcessBuilder processBuilder = new ProcessBuilder(buildArgs());
    processBuilder.environment().clear();
    processBuilder.environment().putAll(environment);

    LOG.debug(
        "Starting buck.py command: %s environment: %s",
        processBuilder.command(),
        processBuilder.environment());
    buckPyProcess = processBuilder.start();
    LOG.debug("Started process %s successfully", buckPyProcess);

    OutputStream stdin = buckPyProcess.getOutputStream();
    InputStream stderr = buckPyProcess.getErrorStream();

    Thread stderrConsumer = Threads.namedThread(
        ProjectBuildFileParser.class.getSimpleName(),
        new InputStreamConsumer(stderr,
            console.getStdErr(),
            console.getAnsi(),
            /* flagOutputWrittenToStream */ true));
    stderrConsumer.start();

    buckPyStdinWriter = new BufferedWriter(new OutputStreamWriter(stdin));

    Reader reader = new InputStreamReader(buckPyProcess.getInputStream(), Charsets.UTF_8);
    buckPyStdoutParser = new BuildFileToJsonParser(reader, isServerMode);
  }

  private ImmutableList<String> buildArgs() throws IOException {
    // Invoking buck.py and read JSON-formatted build rules from its stdout.
    ImmutableList.Builder<String> argBuilder = ImmutableList.builder();

    argBuilder.add(pythonInterpreter);

    // Ask python to unbuffer stdout so that we can coordinate based on the output as it is
    // produced.
    argBuilder.add("-u");

    argBuilder.add(getPathToBuckPy(descriptions).toString());

    if (isServerMode) {
      // Provide BUCK files to parse via buck.py's stdin.
      argBuilder.add("--server");
    }

    argBuilder.add("--project_root", projectRoot.toAbsolutePath().toString());

    // Add the --include flags.
    for (String include : commonIncludes) {
      argBuilder.add("--include");
      argBuilder.add(include);
    }

    for (Path path : ignorePaths) {
      argBuilder.add("--ignore_path");
      argBuilder.add(path.toString());
    }

    return argBuilder.build();
  }

  /**
   * Create, parse and destroy the parser in one step for an entire project.  This should
   * only be used when the tree must be parsed without a specific target to be built or
   * otherwise operated upon.
   */
  public static List<Map<String, Object>> getAllRulesInProject(
      ProjectBuildFileParserFactory factory,
      Iterable<String> includes,
      Console console,
      ImmutableMap<String, String> environment)
      throws BuildFileParseException, InterruptedException {
    try (ProjectBuildFileParser buildFileParser =
             factory.createParser(
                 includes,
                 console,
                 environment)) {
      buildFileParser.setServerMode(false);
      return buildFileParser.getAllRulesInternal(Optional.<Path>absent());
    } catch (IOException e) {
      throw BuildFileParseException.createForGenericBuildFileParseError(e);
    }
  }

  /**
   * Collect all rules from a particular build file.
   *
   * @param buildFile should be an absolute path to a build file. Must have rootPath as its prefix.
   */
  public List<Map<String, Object>> getAllRules(Path buildFile)
      throws BuildFileParseException {
    List<Map<String, Object>> result = getAllRulesAndMetaRules(buildFile);

    // Strip out the __includes meta rule, which is the last rule.
    return Collections.unmodifiableList(result.subList(0, result.size() - 1));
  }

  /**
   * Collect all rules from a particular build file, along with meta rules about the rules, for
   * example which build files the rules depend on.
   *
   * @param buildFile should be an absolute path to a build file. Must have rootPath as its prefix.
   */
  public List<Map<String, Object>> getAllRulesAndMetaRules(Path buildFile)
      throws BuildFileParseException {
    try {
      return getAllRulesInternal(Optional.of(buildFile));
    } catch (IOException e) {
      throw BuildFileParseException.createForBuildFileParseError(buildFile, e);
    }
  }

  @VisibleForTesting
  protected List<Map<String, Object>> getAllRulesInternal(Optional<Path> buildFile)
      throws IOException {
    ensureNotClosed();
    initIfNeeded();

    // Check isInitialized implications (to avoid Eradicate warnings).
    Preconditions.checkNotNull(buckPyStdoutParser);
    Preconditions.checkNotNull(buckPyStdinWriter);
    Preconditions.checkNotNull(buckPyProcess);

    // When in server mode, we require a build file.  When not in server mode, we
    // cannot accept a build file.  Pretty stupid, actually.  Consider fixing this.
    Preconditions.checkState(buildFile.isPresent() == isServerMode);

    if (buildFile.isPresent()) {
      buckPyStdinWriter.write(buildFile.get().toString());
      buckPyStdinWriter.newLine();
      buckPyStdinWriter.flush();
    }

    LOG.debug("Parsing output of process %s...", buckPyProcess);
    List<Map<String, Object>> result = buckPyStdoutParser.nextRules();
    LOG.debug("Parsed %d rules from process", result.size());
    return result;
  }

  @Override
  @SuppressWarnings("PMD.EmptyCatchBlock")
  public void close() throws BuildFileParseException, InterruptedException {
    if (isClosed) {
      return;
    }

    try {
      if (isInitialized) {

        // Check isInitialized implications (to avoid Eradicate warnings).
        Preconditions.checkNotNull(buckPyStdoutParser);
        Preconditions.checkNotNull(buckPyStdinWriter);
        Preconditions.checkNotNull(buckPyProcess);

        try {
          buckPyStdoutParser.close();
        } catch (IOException e) {
          // This is bad, but we swallow this so we can still close the other objects.
        }

        if (isServerMode) {
          // Allow buck.py to terminate gracefully.
          try {
            buckPyStdinWriter.close();
          } catch (IOException e) {
            // Safe to ignore since we've already flushed everything we wanted
            // to write.
          }
        }

        LOG.debug("Waiting for process %s to exit...", buckPyProcess);
        int exitCode = buckPyProcess.waitFor();
        if (exitCode != 0) {
          LOG.error("Process %s exited with error code %d", buckPyProcess, exitCode);
          throw BuildFileParseException.createForUnknownParseError(
              String.format("Parser did not exit cleanly (exit code: %d)", exitCode));
        }
        LOG.debug("Process %s exited cleanly.", buckPyProcess);

        try {
          synchronized (this) {
            if (pathToBuckPy.isPresent()) {
              Files.delete(pathToBuckPy.get());
            }
          }
        } catch (IOException e) {
          // Eat any exceptions from deleting the temporary buck.py file.
        }

      }
    } finally {
      isClosed = true;
    }
  }

  private Path getPathToBuckPy(ImmutableSet<Description<?>> descriptions) throws IOException {
    generatePathToBuckPy(descriptions);
    return pathToBuckPy.get();
  }

  private synchronized void generatePathToBuckPy(ImmutableSet<Description<?>> descriptions)
      throws IOException {
    if (pathToBuckPy.isPresent()) {
      return;
    }

    LOG.debug("Creating temporary buck.py instance...");
    // We currently create a temporary buck.py per instance of this class, rather than a single one
    // for the life of this buck invocation. We do this since this is generated in parallel we end
    // up with strange InterruptedExceptions being thrown.
    // TODO(simons): This would be the ideal thing to do.
    //    Path buckDotPy =
    //        projectRoot.toPath().resolve(BuckConstant.BIN_DIR).resolve("generated-buck.py");
    Path buckDotPy = Files.createTempFile("buck", ".py");
    Files.createDirectories(buckDotPy.getParent());

    try (Writer out = Files.newBufferedWriter(buckDotPy, UTF_8)) {
      Path original = Paths.get(PATH_TO_BUCK_PY);
      CharStreams.copy(Files.newBufferedReader(original, UTF_8), out);
      out.write("\n\n");

      // The base path doesn't matter, but should be set.
      ConstructorArgMarshaller inspector = new ConstructorArgMarshaller(projectRoot);
      BuckPyFunction function = new BuckPyFunction(inspector);
      for (Description<?> description : descriptions) {
        out.write(function.toPythonFunction(
            description.getBuildRuleType(),
            description.createUnpopulatedConstructorArg()));
        out.write('\n');
      }

      out.write(Joiner.on("\n").join(
          "if __name__ == '__main__':",
          "  try:",
          "    main()",
          "  except KeyboardInterrupt:",
          "    print >> sys.stderr, 'Killed by User'",
          ""));
    }
    Path normalizedBuckDotPyPath = buckDotPy.normalize();
    pathToBuckPy = Optional.of(normalizedBuckDotPyPath);
    LOG.debug("Created temporary buck.py instance at %s.", normalizedBuckDotPyPath);
  }
}


text:
package org.elasticsearch.index.replication;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkShardRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.iterable.Iterables;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.engine.EngineFactory;
import org.elasticsearch.index.engine.EngineTestCase;
import org.elasticsearch.index.engine.InternalEngine;
import org.elasticsearch.index.engine.InternalEngineTests;
import org.elasticsearch.index.engine.SegmentsStats;
import org.elasticsearch.index.engine.VersionConflictEngineException;
import org.elasticsearch.index.mapper.SeqNoFieldMapper;
import org.elasticsearch.index.seqno.SeqNoStats;
import org.elasticsearch.index.seqno.SequenceNumbers;
import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.index.shard.IndexShardTests;
import org.elasticsearch.index.store.Store;
import org.elasticsearch.index.translog.SnapshotMatchers;
import org.elasticsearch.index.translog.Translog;
import org.elasticsearch.indices.recovery.RecoveryTarget;
import org.elasticsearch.threadpool.TestThreadPool;
import org.elasticsearch.threadpool.ThreadPool;
import org.hamcrest.Matcher;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.elasticsearch.index.translog.SnapshotMatchers.containsOperationsInAnyOrder;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.either;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;

public class IndexLevelReplicationTests extends ESIndexLevelReplicationTestCase {

    public void testSimpleReplication() throws Exception {
        try (ReplicationGroup shards = createGroup(randomInt(2))) {
            shards.startAll();
            final int docCount = randomInt(50);
            shards.indexDocs(docCount);
            shards.assertAllEqual(docCount);
            for (IndexShard replica : shards.getReplicas()) {
                assertThat(EngineTestCase.getNumVersionLookups(getEngine(replica)), equalTo(0L));
            }
        }
    }

    public void testSimpleAppendOnlyReplication() throws Exception {
        try (ReplicationGroup shards = createGroup(randomInt(2))) {
            shards.startAll();
            final int docCount = randomInt(50);
            shards.appendDocs(docCount);
            shards.assertAllEqual(docCount);
            for (IndexShard replica : shards.getReplicas()) {
                assertThat(EngineTestCase.getNumVersionLookups(getEngine(replica)), equalTo(0L));
            }
        }
    }

    public void testAppendWhileRecovering() throws Exception {
        try (ReplicationGroup shards = createGroup(0)) {
            shards.startAll();
            CountDownLatch latch = new CountDownLatch(2);
            int numDocs = randomIntBetween(100, 200);
            shards.appendDocs(1);// just append one to the translog so we can assert below
            Thread thread = new Thread() {
                @Override
                public void run() {
                    try {
                        latch.countDown();
                        latch.await();
                        shards.appendDocs(numDocs - 1);
                    } catch (Exception e) {
                        throw new AssertionError(e);
                    }
                }
            };
            thread.start();
            IndexShard replica = shards.addReplica();
            Future<Void> future = shards.asyncRecoverReplica(replica,
                (indexShard, node) -> new RecoveryTarget(indexShard, node, recoveryListener) {
                    @Override
                    public void cleanFiles(int totalTranslogOps, long globalCheckpoint,
                                           Store.MetadataSnapshot sourceMetadata, ActionListener<Void> listener) {
                        super.cleanFiles(totalTranslogOps, globalCheckpoint, sourceMetadata, ActionListener.runAfter(listener, () -> {
                            latch.countDown();
                            try {
                                latch.await();
                            } catch (InterruptedException e) {
                                throw new AssertionError(e);
                            }
                        }));
                    }
                });
            future.get();
            thread.join();
            shards.assertAllEqual(numDocs);
            Engine engine = IndexShardTests.getEngineFromShard(shards.getPrimary());
            assertEquals(0, InternalEngineTests.getNumIndexVersionsLookups((InternalEngine) engine));
            assertEquals(0, InternalEngineTests.getNumVersionLookups((InternalEngine) engine));
        }
    }

    public void testRetryAppendOnlyAfterRecovering() throws Exception {
        try (ReplicationGroup shards = createGroup(0)) {
            shards.startAll();
            final IndexRequest originalRequest = new IndexRequest(index.getName()).source("{}", XContentType.JSON);
            originalRequest.process(Version.CURRENT, null, index.getName());
            final IndexRequest retryRequest = copyIndexRequest(originalRequest);
            retryRequest.onRetry();
            shards.index(retryRequest);
            IndexShard replica = shards.addReplica();
            shards.recoverReplica(replica);
            shards.assertAllEqual(1);
            shards.index(originalRequest); // original append-only arrives after recovery completed
            shards.assertAllEqual(1);
            assertThat(replica.getMaxSeenAutoIdTimestamp(), equalTo(originalRequest.getAutoGeneratedTimestamp()));
        }
    }

    public void testAppendOnlyRecoveryThenReplication() throws Exception {
        CountDownLatch indexedOnPrimary = new CountDownLatch(1);
        CountDownLatch recoveryDone = new CountDownLatch(1);
        try (ReplicationGroup shards = new ReplicationGroup(buildIndexMetadata(1)) {
            @Override
            protected EngineFactory getEngineFactory(ShardRouting routing) {
                return config -> new InternalEngine(config) {
                    @Override
                    public IndexResult index(Index op) throws IOException {
                        IndexResult result = super.index(op);
                        if (op.origin() == Operation.Origin.PRIMARY) {
                            indexedOnPrimary.countDown();
                            // prevent the indexing on the primary from returning (it was added to Lucene and translog already)
                            // to make sure that this operation is replicated to the replica via recovery, then via replication.
                            try {
                                recoveryDone.await();
                            } catch (InterruptedException e) {
                                throw new AssertionError(e);
                            }
                        }
                        return result;
                    }
                };
            }
        }) {
            shards.startAll();
            Thread thread = new Thread(() -> {
                IndexRequest indexRequest = new IndexRequest(index.getName()).source("{}", XContentType.JSON);
                try {
                    shards.index(indexRequest);
                } catch (Exception e) {
                    throw new AssertionError(e);
                }
            });
            thread.start();
            IndexShard replica = shards.addReplica();
            Future<Void> fut = shards.asyncRecoverReplica(replica,
                (shard, node) -> new RecoveryTarget(shard, node, recoveryListener) {
                    @Override
                    public void prepareForTranslogOperations(int totalTranslogOps, ActionListener<Void> listener) {
                        try {
                            indexedOnPrimary.await();
                        } catch (InterruptedException e) {
                            throw new AssertionError(e);
                        }
                        super.prepareForTranslogOperations(totalTranslogOps, listener);
                    }
                });
            fut.get();
            recoveryDone.countDown();
            thread.join();
            shards.assertAllEqual(1);
        }
    }

    public void testInheritMaxValidAutoIDTimestampOnRecovery() throws Exception {
        try (ReplicationGroup shards = createGroup(0)) {
            shards.startAll();
            final IndexRequest indexRequest = new IndexRequest(index.getName()).source("{}", XContentType.JSON);
            indexRequest.onRetry(); // force an update of the timestamp
            final BulkItemResponse response = shards.index(indexRequest);
            assertEquals(DocWriteResponse.Result.CREATED, response.getResponse().getResult());
            if (randomBoolean()) { // lets check if that also happens if no translog record is replicated
                shards.flush();
            }
            IndexShard replica = shards.addReplica();
            shards.recoverReplica(replica);

            SegmentsStats segmentsStats = replica.segmentStats(false, false);
            SegmentsStats primarySegmentStats = shards.getPrimary().segmentStats(false, false);
            assertNotEquals(IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, primarySegmentStats.getMaxUnsafeAutoIdTimestamp());
            assertEquals(primarySegmentStats.getMaxUnsafeAutoIdTimestamp(), segmentsStats.getMaxUnsafeAutoIdTimestamp());
            assertNotEquals(Long.MAX_VALUE, segmentsStats.getMaxUnsafeAutoIdTimestamp());
        }
    }

    public void testCheckpointsAdvance() throws Exception {
        try (ReplicationGroup shards = createGroup(randomInt(3))) {
            shards.startPrimary();
            int numDocs = 0;
            int startedShards;
            do {
                numDocs += shards.indexDocs(randomInt(20));
                startedShards = shards.startReplicas(randomIntBetween(1, 2));
            } while (startedShards > 0);

            for (IndexShard shard : shards) {
                final SeqNoStats shardStats = shard.seqNoStats();
                final ShardRouting shardRouting = shard.routingEntry();
                assertThat(shardRouting + " local checkpoint mismatch", shardStats.getLocalCheckpoint(), equalTo(numDocs - 1L));
                /*
                 * After the last indexing operation completes, the primary will advance its global checkpoint. Without another indexing
                 * operation, or a background sync, the primary will not have broadcast this global checkpoint to its replicas. However, a
                 * shard could have recovered from the primary in which case its global checkpoint will be in-sync with the primary.
                 * Therefore, we can only assert that the global checkpoint is number of docs minus one (matching the primary, in case of a
                 * recovery), or number of docs minus two (received indexing operations but has not received a global checkpoint sync after
                 * the last operation completed).
                 */
                final Matcher<Long> globalCheckpointMatcher;
                if (shardRouting.primary()) {
                    globalCheckpointMatcher = numDocs == 0 ? equalTo(SequenceNumbers.NO_OPS_PERFORMED) : equalTo(numDocs - 1L);
                } else {
                    globalCheckpointMatcher = numDocs == 0 ? equalTo(SequenceNumbers.NO_OPS_PERFORMED)
                        : anyOf(equalTo(numDocs - 1L), equalTo(numDocs - 2L));
                }
                assertThat(shardRouting + " global checkpoint mismatch", shardStats.getGlobalCheckpoint(), globalCheckpointMatcher);
                assertThat(shardRouting + " max seq no mismatch", shardStats.getMaxSeqNo(), equalTo(numDocs - 1L));
            }

            // simulate a background global checkpoint sync at which point we expect the global checkpoint to advance on the replicas
            shards.syncGlobalCheckpoint();

            final long noOpsPerformed = SequenceNumbers.NO_OPS_PERFORMED;
            for (IndexShard shard : shards) {
                final SeqNoStats shardStats = shard.seqNoStats();
                final ShardRouting shardRouting = shard.routingEntry();
                assertThat(shardRouting + " local checkpoint mismatch", shardStats.getLocalCheckpoint(), equalTo(numDocs - 1L));
                assertThat(
                        shardRouting + " global checkpoint mismatch",
                        shardStats.getGlobalCheckpoint(),
                        numDocs == 0 ? equalTo(noOpsPerformed) : equalTo(numDocs - 1L));
                assertThat(shardRouting + " max seq no mismatch", shardStats.getMaxSeqNo(), equalTo(numDocs - 1L));
            }
        }
    }

    public void testConflictingOpsOnReplica() throws Exception {
        String mappings = "{ \"_doc\": { \"properties\": { \"f\": { \"type\": \"keyword\"} }}}";
        try (ReplicationGroup shards = new ReplicationGroup(buildIndexMetadata(2, mappings))) {
            shards.startAll();
            List<IndexShard> replicas = shards.getReplicas();
            IndexShard replica1 = replicas.get(0);
            IndexRequest indexRequest = new IndexRequest(index.getName()).id("1").source("{ \"f\": \"1\"}", XContentType.JSON);
            logger.info("--> isolated replica " + replica1.routingEntry());
            BulkShardRequest replicationRequest = indexOnPrimary(indexRequest, shards.getPrimary());
            for (int i = 1; i < replicas.size(); i++) {
                indexOnReplica(replicationRequest, shards, replicas.get(i));
            }

            logger.info("--> promoting replica to primary " + replica1.routingEntry());
            shards.promoteReplicaToPrimary(replica1).get();
            indexRequest = new IndexRequest(index.getName()).id("1").source("{ \"f\": \"2\"}", XContentType.JSON);
            shards.index(indexRequest);
            shards.refresh("test");
            for (IndexShard shard : shards) {
                try (Engine.Searcher searcher = shard.acquireSearcher("test")) {
                    TopDocs search = searcher.search(new TermQuery(new Term("f", "2")), 10);
                    assertEquals("shard " + shard.routingEntry() + " misses new version", 1, search.totalHits.value);
                }
            }
        }
    }

    public void testReplicaTermIncrementWithConcurrentPrimaryPromotion() throws Exception {
        String mappings = "{ \"_doc\": { \"properties\": { \"f\": { \"type\": \"keyword\"} }}}";
        try (ReplicationGroup shards = new ReplicationGroup(buildIndexMetadata(2, mappings))) {
            shards.startAll();
            long primaryPrimaryTerm = shards.getPrimary().getPendingPrimaryTerm();
            List<IndexShard> replicas = shards.getReplicas();
            IndexShard replica1 = replicas.get(0);
            IndexShard replica2 = replicas.get(1);

            shards.promoteReplicaToPrimary(replica1, (shard, listener) -> {});
            long newReplica1Term = replica1.getPendingPrimaryTerm();
            assertEquals(primaryPrimaryTerm + 1, newReplica1Term);

            assertEquals(primaryPrimaryTerm, replica2.getPendingPrimaryTerm());

            IndexRequest indexRequest = new IndexRequest(index.getName()).id("1").source("{ \"f\": \"1\"}", XContentType.JSON);
            BulkShardRequest replicationRequest = indexOnPrimary(indexRequest, replica1);

            CyclicBarrier barrier = new CyclicBarrier(2);
            Thread t1 = new Thread(() -> {
                try {
                    barrier.await();
                    indexOnReplica(replicationRequest, shards, replica2, newReplica1Term);
                } catch (IllegalStateException ise) {
                    assertThat(ise.getMessage(), either(containsString("is too old"))
                        .or(containsString("cannot be a replication target")).or(containsString("engine is closed")));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
            Thread t2 = new Thread(() -> {
                try {
                    barrier.await();
                    shards.promoteReplicaToPrimary(replica2).get();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
            t2.start();
            t1.start();
            t1.join();
            t2.join();

            assertEquals(newReplica1Term + 1, replica2.getPendingPrimaryTerm());
        }
    }

    public void testReplicaOperationWithConcurrentPrimaryPromotion() throws Exception {
        String mappings = "{ \"_doc\": { \"properties\": { \"f\": { \"type\": \"keyword\"} }}}";
        try (ReplicationGroup shards = new ReplicationGroup(buildIndexMetadata(1, mappings))) {
            shards.startAll();
            long primaryPrimaryTerm = shards.getPrimary().getPendingPrimaryTerm();
            IndexRequest indexRequest = new IndexRequest(index.getName()).id("1").source("{ \"f\": \"1\"}", XContentType.JSON);
            BulkShardRequest replicationRequest = indexOnPrimary(indexRequest, shards.getPrimary());

            List<IndexShard> replicas = shards.getReplicas();
            IndexShard replica = replicas.get(0);

            CyclicBarrier barrier = new CyclicBarrier(2);
            AtomicBoolean successFullyIndexed = new AtomicBoolean();
            Thread t1 = new Thread(() -> {
                try {
                    barrier.await();
                    indexOnReplica(replicationRequest, shards, replica, primaryPrimaryTerm);
                    successFullyIndexed.set(true);
                } catch (IllegalStateException ise) {
                    assertThat(ise.getMessage(), either(containsString("is too old"))
                        .or(containsString("cannot be a replication target")).or(containsString("engine is closed")));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
            Thread t2 = new Thread(() -> {
                try {
                    barrier.await();
                    shards.promoteReplicaToPrimary(replica).get();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
            t2.start();
            t1.start();
            t1.join();
            t2.join();

            assertEquals(primaryPrimaryTerm + 1, replica.getPendingPrimaryTerm());
            if (successFullyIndexed.get()) {
                try(Translog.Snapshot snapshot = getTranslog(replica).newSnapshot()) {
                    assertThat(snapshot.totalOperations(), equalTo(1));
                    Translog.Operation op = snapshot.next();
                    assertThat(op.primaryTerm(), equalTo(primaryPrimaryTerm));
                }
            }
        }
    }

    /**
     * test document failures (failures after seq_no generation) are added as noop operation to the translog
     * for primary and replica shards
     */
    public void testDocumentFailureReplication() throws Exception {
        final IOException indexException = new IOException("simulated indexing failure");
        final EngineFactory engineFactory = config -> InternalEngineTests.createInternalEngine((dir, iwc) ->
            new IndexWriter(dir, iwc) {
                @Override
                public long addDocument(Iterable<? extends IndexableField> doc) throws IOException {
                    boolean isTombstone = false;
                    for (IndexableField field : doc) {
                        if (SeqNoFieldMapper.TOMBSTONE_NAME.equals(field.name())) {
                            isTombstone = true;
                        }
                    }
                    if (isTombstone) {
                        return super.addDocument(doc); // allow to add Noop
                    } else {
                        throw indexException;
                    }
                }
            }, null, null, config);
        try (ReplicationGroup shards = new ReplicationGroup(buildIndexMetadata(0)) {
            @Override
            protected EngineFactory getEngineFactory(ShardRouting routing) { return engineFactory; }}) {

            // start with the primary only so two first failures are replicated to replicas via recovery from the translog of the primary.
            shards.startPrimary();
            long primaryTerm = shards.getPrimary().getPendingPrimaryTerm();
            List<Translog.Operation> expectedTranslogOps = new ArrayList<>();
            BulkItemResponse indexResp = shards.index(new IndexRequest(index.getName()).id("1").source("{}", XContentType.JSON));
            assertThat(indexResp.isFailed(), equalTo(true));
            assertThat(indexResp.getFailure().getCause(), equalTo(indexException));
            expectedTranslogOps.add(new Translog.NoOp(0, primaryTerm, indexException.toString()));
            try (Translog.Snapshot snapshot = getTranslog(shards.getPrimary()).newSnapshot()) {
                assertThat(snapshot, SnapshotMatchers.containsOperationsInAnyOrder(expectedTranslogOps));
            }
            shards.assertAllEqual(0);

            int nReplica = randomIntBetween(1, 3);
            for (int i = 0; i < nReplica; i++) {
                shards.addReplica();
            }
            shards.startReplicas(nReplica);
            for (IndexShard shard : shards) {
                try (Translog.Snapshot snapshot = getTranslog(shard).newSnapshot()) {
                    // we flush at the end of peer recovery
                    if (shard.routingEntry().primary() || shard.indexSettings().isSoftDeleteEnabled() == false) {
                        assertThat(snapshot, SnapshotMatchers.containsOperationsInAnyOrder(expectedTranslogOps));
                    } else {
                        assertThat(snapshot.totalOperations(), equalTo(0));
                    }
                }
                try (Translog.Snapshot snapshot = shard.newChangesSnapshot("test", 0, Long.MAX_VALUE, false)) {
                    assertThat(snapshot, SnapshotMatchers.containsOperationsInAnyOrder(expectedTranslogOps));
                }
            }
            // the failure replicated directly from the replication channel.
            indexResp = shards.index(new IndexRequest(index.getName()).id("any").source("{}", XContentType.JSON));
            assertThat(indexResp.getFailure().getCause(), equalTo(indexException));
            Translog.NoOp noop2 = new Translog.NoOp(1, primaryTerm, indexException.toString());
            expectedTranslogOps.add(noop2);

            for (IndexShard shard : shards) {
                try (Translog.Snapshot snapshot = getTranslog(shard).newSnapshot()) {
                    if (shard.routingEntry().primary() || shard.indexSettings().isSoftDeleteEnabled() == false) {
                        assertThat(snapshot, SnapshotMatchers.containsOperationsInAnyOrder(expectedTranslogOps));
                    } else {
                        assertThat(snapshot, SnapshotMatchers.containsOperationsInAnyOrder(Collections.singletonList(noop2)));
                    }
                }
                try (Translog.Snapshot snapshot = shard.newChangesSnapshot("test", 0, Long.MAX_VALUE, false)) {
                    assertThat(snapshot, SnapshotMatchers.containsOperationsInAnyOrder(expectedTranslogOps));
                }
            }
            shards.assertAllEqual(0);
        }
    }

    /**
     * test request failures (failures before seq_no generation) are not added as a noop to translog
     */
    public void testRequestFailureReplication() throws Exception {
        try (ReplicationGroup shards = createGroup(0)) {
            shards.startAll();
            BulkItemResponse response = shards.index(
                    new IndexRequest(index.getName()).id("1")
                            .source("{}", XContentType.JSON)
                            .version(2)
            );
            assertTrue(response.isFailed());
            assertThat(response.getFailure().getCause(), instanceOf(VersionConflictEngineException.class));
            shards.assertAllEqual(0);
            for (IndexShard indexShard : shards) {
                assertThat(indexShard.routingEntry() + " has the wrong number of ops in the translog",
                    indexShard.translogStats().estimatedNumberOfOperations(), equalTo(0));
            }

            // add some replicas
            int nReplica = randomIntBetween(1, 3);
            for (int i = 0; i < nReplica; i++) {
                shards.addReplica();
            }
            shards.startReplicas(nReplica);
            response = shards.index(
                    new IndexRequest(index.getName()).id("1")
                            .source("{}", XContentType.JSON)
                            .version(2)
            );
            assertTrue(response.isFailed());
            assertThat(response.getFailure().getCause(), instanceOf(VersionConflictEngineException.class));
            shards.assertAllEqual(0);
            for (IndexShard indexShard : shards) {
                assertThat(indexShard.routingEntry() + " has the wrong number of ops in the translog",
                    indexShard.translogStats().estimatedNumberOfOperations(), equalTo(0));
            }
        }
    }

    public void testSeqNoCollision() throws Exception {
        try (ReplicationGroup shards = createGroup(2)) {
            shards.startAll();
            int initDocs = shards.indexDocs(randomInt(10));
            List<IndexShard> replicas = shards.getReplicas();
            IndexShard replica1 = replicas.get(0);
            IndexShard replica2 = replicas.get(1);
            shards.syncGlobalCheckpoint();

            logger.info("--> Isolate replica1");
            IndexRequest indexDoc1 = new IndexRequest(index.getName()).id("d1").source("{}", XContentType.JSON);
            BulkShardRequest replicationRequest = indexOnPrimary(indexDoc1, shards.getPrimary());
            indexOnReplica(replicationRequest, shards, replica2);

            final Translog.Operation op1;
            final List<Translog.Operation> initOperations = new ArrayList<>(initDocs);
            try (Translog.Snapshot snapshot = getTranslog(replica2).newSnapshot()) {
                assertThat(snapshot.totalOperations(), equalTo(initDocs + 1));
                for (int i = 0; i < initDocs; i++) {
                    Translog.Operation op = snapshot.next();
                    assertThat(op, is(notNullValue()));
                    initOperations.add(op);
                }
                op1 = snapshot.next();
                assertThat(op1, notNullValue());
                assertThat(snapshot.next(), nullValue());
                assertThat(snapshot.skippedOperations(), equalTo(0));
            }
            logger.info("--> Promote replica1 as the primary");
            shards.promoteReplicaToPrimary(replica1).get(); // wait until resync completed.
            shards.index(new IndexRequest(index.getName()).id("d2").source("{}", XContentType.JSON));
            final Translog.Operation op2;
            try (Translog.Snapshot snapshot = getTranslog(replica2).newSnapshot()) {
                assertThat(snapshot.totalOperations(), equalTo(1));
                op2 = snapshot.next();
                assertThat(op2.seqNo(), equalTo(op1.seqNo()));
                assertThat(op2.primaryTerm(), greaterThan(op1.primaryTerm()));
                assertNull(snapshot.next());
                assertThat(snapshot.skippedOperations(), equalTo(0));
            }

            // Make sure that peer-recovery transfers all but non-overridden operations.
            IndexShard replica3 = shards.addReplica();
            logger.info("--> Promote replica2 as the primary");
            shards.promoteReplicaToPrimary(replica2).get();
            logger.info("--> Recover replica3 from replica2");
            recoverReplica(replica3, replica2, true);
            try (Translog.Snapshot snapshot = replica3.newChangesSnapshot("test", 0, Long.MAX_VALUE, false)) {
                assertThat(snapshot.totalOperations(), equalTo(initDocs + 1));
                final List<Translog.Operation> expectedOps = new ArrayList<>(initOperations);
                expectedOps.add(op2);
                assertThat(snapshot, containsOperationsInAnyOrder(expectedOps));
                assertThat("Peer-recovery should not send overridden operations", snapshot.skippedOperations(), equalTo(0));
            }
            shards.assertAllEqual(initDocs + 1);
        }
    }

    /**
     * This test ensures the consistency between primary and replica with late and out of order delivery on the replica.
     * An index operation on the primary is followed by a delete operation. The delete operation is delivered first
     * and processed on the replica but the index is delayed with an interval that is even longer the gc deletes cycle.
     * This makes sure that that replica still remembers the delete operation and correctly ignores the stale index operation.
     */
    public void testLateDeliveryAfterGCTriggeredOnReplica() throws Exception {
        ThreadPool.terminate(this.threadPool, 10, TimeUnit.SECONDS);
        this.threadPool = new TestThreadPool(getClass().getName(),
            Settings.builder().put(threadPoolSettings()).put(ThreadPool.ESTIMATED_TIME_INTERVAL_SETTING.getKey(), 0).build());

        try (ReplicationGroup shards = createGroup(1)) {
            shards.startAll();
            final IndexShard primary = shards.getPrimary();
            final IndexShard replica = shards.getReplicas().get(0);
            final TimeValue gcInterval = TimeValue.timeValueMillis(between(1, 10));
            // I think we can just set this to something very small (10ms?) and also set ThreadPool#ESTIMATED_TIME_INTERVAL_SETTING to 0?

            updateGCDeleteCycle(replica, gcInterval);
            final BulkShardRequest indexRequest = indexOnPrimary(
                new IndexRequest(index.getName()).id("d1").source("{}", XContentType.JSON), primary);
            final BulkShardRequest deleteRequest = deleteOnPrimary(new DeleteRequest(index.getName()).id("d1"), primary);
            deleteOnReplica(deleteRequest, shards, replica); // delete arrives on replica first.
            final long deleteTimestamp = threadPool.relativeTimeInMillis();
            replica.refresh("test");
            assertBusy(() ->
                assertThat(threadPool.relativeTimeInMillis() - deleteTimestamp, greaterThan(gcInterval.millis()))
            );
            getEngine(replica).maybePruneDeletes();
            indexOnReplica(indexRequest, shards, replica);  // index arrives on replica lately.
            shards.assertAllEqual(0);
        }
    }

    private void updateGCDeleteCycle(IndexShard shard, TimeValue interval) {
        IndexMetadata.Builder builder = IndexMetadata.builder(shard.indexSettings().getIndexMetadata());
        builder.settings(Settings.builder()
            .put(shard.indexSettings().getSettings())
            .put(IndexSettings.INDEX_GC_DELETES_SETTING.getKey(), interval.getStringRep())
        );
        shard.indexSettings().updateIndexMetadata(builder.build());
        shard.onSettingsChanged();
    }

    /**
     * This test ensures the consistency between primary and replica when non-append-only (eg. index request with id or delete) operation
     * of the same document is processed before the original append-only request on replicas. The append-only document can be exposed and
     * deleted on the primary before it is added to replica. Replicas should treat a late append-only request as a regular index request.
     */
    public void testOutOfOrderDeliveryForAppendOnlyOperations() throws Exception {
        try (ReplicationGroup shards = createGroup(1)) {
            shards.startAll();
            final IndexShard primary = shards.getPrimary();
            final IndexShard replica = shards.getReplicas().get(0);
            // Append-only request - without id
            final BulkShardRequest indexRequest = indexOnPrimary(
                new IndexRequest(index.getName()).source("{}", XContentType.JSON), primary);
            final String docId = Iterables.get(getShardDocUIDs(primary), 0);
            final BulkShardRequest deleteRequest = deleteOnPrimary(new DeleteRequest(index.getName()).id(docId), primary);
            deleteOnReplica(deleteRequest, shards, replica);
            indexOnReplica(indexRequest, shards, replica);
            shards.assertAllEqual(0);
        }
    }

    public void testIndexingOptimizationUsingSequenceNumbers() throws Exception {
        final Set<String> liveDocs = new HashSet<>();
        try (ReplicationGroup group = createGroup(2)) {
            group.startAll();
            int numDocs = randomIntBetween(1, 100);
            long versionLookups = 0;
            for (int i = 0; i < numDocs; i++) {
                String id = Integer.toString(randomIntBetween(1, 100));
                if (randomBoolean()) {
                    group.index(new IndexRequest(index.getName()).id(id).source("{}", XContentType.JSON));
                    if (liveDocs.add(id) == false) {
                        versionLookups++;
                    }
                } else {
                    group.delete(new DeleteRequest(index.getName()).id(id));
                    liveDocs.remove(id);
                    versionLookups++;
                }
            }
            for (IndexShard replica : group.getReplicas()) {
                assertThat(EngineTestCase.getNumVersionLookups(getEngine(replica)), equalTo(versionLookups));
            }
        }
    }
}


text:
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// 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.

/**
 * @fileoverview A class representing menu items that open a submenu.
 * @see goog.ui.Menu
 *
 * @see ../demos/submenus.html
 * @see ../demos/submenus2.html
 */

goog.provide('goog.ui.SubMenu');

goog.require('goog.Timer');
goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.dom.classlist');
goog.require('goog.events.KeyCodes');
goog.require('goog.positioning.AnchoredViewportPosition');
goog.require('goog.positioning.Corner');
goog.require('goog.style');
goog.require('goog.ui.Component');
goog.require('goog.ui.Menu');
goog.require('goog.ui.MenuItem');
goog.require('goog.ui.SubMenuRenderer');
goog.require('goog.ui.registry');

/**
 * Class representing a submenu that can be added as an item to other menus.
 *
 * @param {goog.ui.ControlContent} content Text caption or DOM structure to
 *     display as the content of the submenu (use to add icons or styling to
 *     menus).
 * @param {*=} opt_model Data/model associated with the menu item.
 * @param {goog.dom.DomHelper=} opt_domHelper Optional dom helper used for dom
 *     interactions.
 * @param {goog.ui.MenuItemRenderer=} opt_renderer Renderer used to render or
 *     decorate the component; defaults to {@link goog.ui.SubMenuRenderer}.
 * @constructor
 * @extends {goog.ui.MenuItem}
 */
goog.ui.SubMenu = function(content, opt_model, opt_domHelper, opt_renderer) {
  goog.ui.MenuItem.call(
      this, content, opt_model, opt_domHelper,
      opt_renderer || goog.ui.SubMenuRenderer.getInstance());
};
goog.inherits(goog.ui.SubMenu, goog.ui.MenuItem);
goog.tagUnsealableClass(goog.ui.SubMenu);

/**
 * The delay before opening the sub menu in milliseconds.
 * @type {number}
 */
goog.ui.SubMenu.MENU_DELAY_MS = 218;

/**
 * Timer used to dismiss the submenu when the item becomes unhighlighted.
 * @type {?number}
 * @private
 */
goog.ui.SubMenu.prototype.dismissTimer_ = null;

/**
 * Timer used to show the submenu on mouseover.
 * @type {?number}
 * @private
 */
goog.ui.SubMenu.prototype.showTimer_ = null;

/**
 * Whether the submenu believes the menu is visible.
 * @type {boolean}
 * @private
 */
goog.ui.SubMenu.prototype.menuIsVisible_ = false;

/**
 * The lazily created sub menu.
 * @type {goog.ui.Menu?}
 * @private
 */
goog.ui.SubMenu.prototype.subMenu_ = null;

/**
 * Whether or not the sub-menu was set explicitly.
 * @type {boolean}
 * @private
 */
goog.ui.SubMenu.prototype.externalSubMenu_ = false;

/**
 * Whether or not to align the submenu at the end of the parent menu.
 * If true, the menu expands to the right in LTR languages and to the left
 * in RTL langauges.
 * @type {boolean}
 * @private
 */
goog.ui.SubMenu.prototype.alignToEnd_ = true;

/**
 * Whether the position of this submenu may be adjusted to fit
 * the visible area, as in {@link goog.ui.Popup.positionAtCoordinate}.
 * @type {boolean}
 * @private
 */
goog.ui.SubMenu.prototype.isPositionAdjustable_ = false;

/** @override */
goog.ui.SubMenu.prototype.enterDocument = function() {
  goog.ui.SubMenu.superClass_.enterDocument.call(this);

  this.getHandler().listen(
      this.getParent(), goog.ui.Component.EventType.HIDE, this.onParentHidden_);

  if (this.subMenu_) {
    this.setMenuListenersEnabled_(this.subMenu_, true);
  }
};

/** @override */
goog.ui.SubMenu.prototype.exitDocument = function() {
  this.getHandler().unlisten(
      this.getParent(), goog.ui.Component.EventType.HIDE, this.onParentHidden_);

  if (this.subMenu_) {
    this.setMenuListenersEnabled_(this.subMenu_, false);
    if (!this.externalSubMenu_) {
      this.subMenu_.exitDocument();
      goog.dom.removeNode(this.subMenu_.getElement());
    }
  }

  goog.ui.SubMenu.superClass_.exitDocument.call(this);
};

/** @override */
goog.ui.SubMenu.prototype.disposeInternal = function() {
  if (this.subMenu_ && !this.externalSubMenu_) {
    this.subMenu_.dispose();
  }
  this.subMenu_ = null;
  goog.ui.SubMenu.superClass_.disposeInternal.call(this);
};

/**
 * @override
 * Dismisses the submenu on a delay, with the result that the user needs less
 * accuracy when moving to submenus.  Alternate implementations could use
 * geometry instead of a timer.
 * @param {boolean} highlight Whether item should be highlighted.
 * @param {boolean=} opt_btnPressed Whether the mouse button is held down.
 */
goog.ui.SubMenu.prototype.setHighlighted = function(highlight, opt_btnPressed) {
  goog.ui.SubMenu.superClass_.setHighlighted.call(this, highlight);

  if (opt_btnPressed) {
    this.getMenu().setMouseButtonPressed(true);
  }

  if (!highlight) {
    if (this.dismissTimer_) {
      goog.Timer.clear(this.dismissTimer_);
    }
    this.dismissTimer_ = goog.Timer.callOnce(
        this.dismissSubMenu, goog.ui.SubMenu.MENU_DELAY_MS, this);
  }
};

/**
 * Show the submenu and ensure that all siblings are hidden.
 */
goog.ui.SubMenu.prototype.showSubMenu = function() {
  // Only show the menu if this item is still selected. This is called on a
  // timeout, so make sure our parent still exists.
  var parent = this.getParent();
  if (parent && parent.getHighlighted() == this) {
    this.setSubMenuVisible_(true);
    this.dismissSiblings_();
  }
};

/**
 * Dismisses the menu and all further submenus.
 */
goog.ui.SubMenu.prototype.dismissSubMenu = function() {
  // Because setHighlighted calls this function on a timeout, we need to make
  // sure that the sub menu hasn't been disposed when we come back.
  var subMenu = this.subMenu_;
  if (subMenu && subMenu.getParent() == this) {
    this.setSubMenuVisible_(false);
    subMenu.forEachChild(function(child) {
      if (typeof child.dismissSubMenu == 'function') {
        child.dismissSubMenu();
      }
    });
  }
};

/**
 * Clears the show and hide timers for the sub menu.
 */
goog.ui.SubMenu.prototype.clearTimers = function() {
  if (this.dismissTimer_) {
    goog.Timer.clear(this.dismissTimer_);
  }
  if (this.showTimer_) {
    goog.Timer.clear(this.showTimer_);
  }
};

/**
 * Sets the menu item to be visible or invisible.
 * @param {boolean} visible Whether to show or hide the component.
 * @param {boolean=} opt_force If true, doesn't check whether the component
 *     already has the requested visibility, and doesn't dispatch any events.
 * @return {boolean} Whether the visibility was changed.
 * @override
 */
goog.ui.SubMenu.prototype.setVisible = function(visible, opt_force) {
  var visibilityChanged =
      goog.ui.SubMenu.superClass_.setVisible.call(this, visible, opt_force);
  // For menus that allow menu items to be hidden (i.e. ComboBox) ensure that
  // the submenu is hidden.
  if (visibilityChanged && !this.isVisible()) {
    this.dismissSubMenu();
  }
  return visibilityChanged;
};

/**
 * Dismiss all the sub menus of sibling menu items.
 * @private
 */
goog.ui.SubMenu.prototype.dismissSiblings_ = function() {
  this.getParent().forEachChild(function(child) {
    if (child != this && typeof child.dismissSubMenu == 'function') {
      child.dismissSubMenu();
      child.clearTimers();
    }
  }, this);
};

/**
 * Handles a key event that is passed to the menu item from its parent because
 * it is highlighted.  If the right key is pressed the sub menu takes control
 * and delegates further key events to its menu until it is dismissed OR the
 * left key is pressed.
 * @param {goog.events.KeyEvent} e A key event.
 * @return {boolean} Whether the event was handled.
 * @override
 */
goog.ui.SubMenu.prototype.handleKeyEvent = function(e) {
  var keyCode = e.keyCode;
  var openKeyCode = this.isRightToLeft() ? goog.events.KeyCodes.LEFT :
                                           goog.events.KeyCodes.RIGHT;
  var closeKeyCode = this.isRightToLeft() ? goog.events.KeyCodes.RIGHT :
                                            goog.events.KeyCodes.LEFT;

  if (!this.menuIsVisible_) {
    // Menu item doesn't have keyboard control and the right key was pressed.
    // So open take keyboard control and open the sub menu.
    if (this.isEnabled() &&
        (keyCode == openKeyCode || keyCode == this.getMnemonic())) {
      this.showSubMenu();
      this.getMenu().highlightFirst();
      this.clearTimers();

      // The menu item doesn't currently care about the key events so let the
      // parent menu handle them accordingly .
    } else {
      return false;
    }

    // Menu item has control, so let its menu try to handle the keys (this may
    // in turn be handled by sub-sub menus).
  } else if (this.getMenu().handleKeyEvent(e)) {
    // Nothing to do

    // The menu has control and the key hasn't yet been handled, on left arrow
    // we turn off key control.
  } else if (keyCode == closeKeyCode) {
    this.dismissSubMenu();

  } else {
    // Submenu didn't handle the key so let the parent decide what to do.
    return false;
  }

  e.preventDefault();
  return true;
};

/**
 * Listens to the sub menus items and ensures that this menu item is selected
 * while dismissing the others.  This handles the case when the user mouses
 * over other items on their way to the sub menu.
 * @param {goog.events.Event} e Enter event to handle.
 * @private
 */
goog.ui.SubMenu.prototype.onChildEnter_ = function(e) {
  if (this.subMenu_.getParent() == this) {
    this.clearTimers();
    this.getParentEventTarget().setHighlighted(this);
    this.dismissSiblings_();
  }
};

/**
 * Listens to the parent menu's hide event and ensures that all submenus are
 * hidden at the same time.
 * @param {goog.events.Event} e The event.
 * @private
 */
goog.ui.SubMenu.prototype.onParentHidden_ = function(e) {
  // Ignore propagated events
  if (e.target == this.getParentEventTarget()) {
    // TODO(user): Using an event for this is expensive.  Consider having a
    // generalized interface that the parent menu calls on its children when
    // it is hidden.
    this.dismissSubMenu();
    this.clearTimers();
  }
};

/**
 * @override
 * Sets a timer to show the submenu and then dispatches an ENTER event to the
 * parent menu.
 * @param {goog.events.BrowserEvent} e Mouse event to handle.
 */
goog.ui.SubMenu.prototype.handleMouseOver = function(e) {
  if (this.isEnabled()) {
    this.clearTimers();
    this.showTimer_ = goog.Timer.callOnce(
        this.showSubMenu, goog.ui.SubMenu.MENU_DELAY_MS, this);
  }
  goog.ui.SubMenu.superClass_.handleMouseOver.call(this, e);
};

/**
 * Overrides the default mouseup event handler, so that the ACTION isn't
 * dispatched for the submenu itself, instead the submenu is shown instantly.
 * @param {goog.events.Event} e The browser event.
 * @return {boolean} True if the action was allowed to proceed, false otherwise.
 * @override
 */
goog.ui.SubMenu.prototype.performActionInternal = function(e) {
  this.clearTimers();
  var shouldHandleClick =
      this.isSupportedState(goog.ui.Component.State.SELECTED) ||
      this.isSupportedState(goog.ui.Component.State.CHECKED);
  if (shouldHandleClick) {
    return goog.ui.SubMenu.superClass_.performActionInternal.call(this, e);
  } else {
    this.showSubMenu();
    return true;
  }
};

/**
 * Sets the visiblility of the sub menu.
 * @param {boolean} visible Whether to show menu.
 * @private
 */
goog.ui.SubMenu.prototype.setSubMenuVisible_ = function(visible) {
  // Unhighlighting the menuitems if closing the menu so the event handlers can
  // determine the correct state.
  if (!visible && this.getMenu()) {
    this.getMenu().setHighlightedIndex(-1);
  }

  // Dispatch OPEN event before calling getMenu(), so we can create the menu
  // lazily on first access.
  this.dispatchEvent(
      goog.ui.Component.getStateTransitionEvent(
          goog.ui.Component.State.OPENED, visible));
  var subMenu = this.getMenu();
  if (visible != this.menuIsVisible_) {
    goog.dom.classlist.enable(
        goog.asserts.assert(this.getElement()),
        goog.getCssName('goog-submenu-open'), visible);
  }
  if (visible != subMenu.isVisible()) {
    if (visible) {
      // Lazy-render menu when first shown, if needed.
      if (!subMenu.isInDocument()) {
        subMenu.render();
      }
      subMenu.setHighlightedIndex(-1);
    }
    subMenu.setVisible(visible);
    // We must position after the menu is visible, otherwise positioning logic
    // breaks in RTL.
    if (visible) {
      this.positionSubMenu();
    }
  }
  this.menuIsVisible_ = visible;
};

/**
 * Attaches or detaches menu event listeners to/from the given menu.  Called
 * each time a menu is attached to or detached from the submenu.
 * @param {goog.ui.Menu} menu Menu on which to listen for events.
 * @param {boolean} attach Whether to attach or detach event listeners.
 * @private
 */
goog.ui.SubMenu.prototype.setMenuListenersEnabled_ = function(menu, attach) {
  var handler = this.getHandler();
  var method = attach ? handler.listen : handler.unlisten;
  method.call(
      handler, menu, goog.ui.Component.EventType.ENTER, this.onChildEnter_);
};

/**
 * Sets whether the submenu is aligned at the end of the parent menu.
 * @param {boolean} alignToEnd True to align to end, false to align to start.
 */
goog.ui.SubMenu.prototype.setAlignToEnd = function(alignToEnd) {
  if (alignToEnd != this.alignToEnd_) {
    this.alignToEnd_ = alignToEnd;
    if (this.isInDocument()) {
      // Completely re-render the widget.
      var oldElement = this.getElement();
      this.exitDocument();

      if (oldElement.nextSibling) {
        this.renderBefore(/** @type {!Element} */ (oldElement.nextSibling));
      } else {
        this.render(/** @type {Element} */ (oldElement.parentNode));
      }
    }
  }
};

/**
 * Determines whether the submenu is aligned at the end of the parent menu.
 * @return {boolean} True if aligned to the end (the default), false if
 *     aligned to the start.
 */
goog.ui.SubMenu.prototype.isAlignedToEnd = function() {
  return this.alignToEnd_;
};

/**
 * Positions the submenu. This method should be called if the sub menu is
 * opened and the menu element's size changes (e.g., when adding/removing items
 * to an opened sub menu).
 */
goog.ui.SubMenu.prototype.positionSubMenu = function() {
  var position = new goog.positioning.AnchoredViewportPosition(
      this.getElement(),
      this.isAlignedToEnd() ? goog.positioning.Corner.TOP_END :
                              goog.positioning.Corner.TOP_START,
      this.isPositionAdjustable_);

  // TODO(user): Clean up popup code and have this be a one line call
  var subMenu = this.getMenu();
  var el = subMenu.getElement();
  if (!subMenu.isVisible()) {
    el.style.visibility = 'hidden';
    goog.style.setElementShown(el, true);
  }

  position.reposition(
      el, this.isAlignedToEnd() ? goog.positioning.Corner.TOP_START :
                                  goog.positioning.Corner.TOP_END);

  if (!subMenu.isVisible()) {
    goog.style.setElementShown(el, false);
    el.style.visibility = 'visible';
  }
};

// Methods delegated to sub-menu but accessible here for convinience

/**
 * Adds a new menu item at the end of the menu.
 * @param {goog.ui.MenuHeader|goog.ui.MenuItem|goog.ui.MenuSeparator} item Menu
 *     item to add to the menu.
 */
goog.ui.SubMenu.prototype.addItem = function(item) {
  this.getMenu().addChild(item, true);
};

/**
 * Adds a new menu item at a specific index in the menu.
 * @param {goog.ui.MenuHeader|goog.ui.MenuItem|goog.ui.MenuSeparator} item Menu
 *     item to add to the menu.
 * @param {number} n Index at which to insert the menu item.
 */
goog.ui.SubMenu.prototype.addItemAt = function(item, n) {
  this.getMenu().addChildAt(item, n, true);
};

/**
 * Removes an item from the menu and disposes it.
 * @param {goog.ui.MenuItem} item The menu item to remove.
 */
goog.ui.SubMenu.prototype.removeItem = function(item) {
  var child = this.getMenu().removeChild(item, true);
  if (child) {
    child.dispose();
  }
};

/**
 * Removes a menu item at a given index in the menu and disposes it.
 * @param {number} n Index of item.
 */
goog.ui.SubMenu.prototype.removeItemAt = function(n) {
  var child = this.getMenu().removeChildAt(n, true);
  if (child) {
    child.dispose();
  }
};

/**
 * Returns a reference to the menu item at a given index.
 * @param {number} n Index of menu item.
 * @return {goog.ui.Component} Reference to the menu item.
 */
goog.ui.SubMenu.prototype.getItemAt = function(n) {
  return this.getMenu().getChildAt(n);
};

/**
 * Returns the number of items in the sub menu (including separators).
 * @return {number} The number of items in the menu.
 */
goog.ui.SubMenu.prototype.getItemCount = function() {
  return this.getMenu().getChildCount();
};

/**
 * Returns the menu items contained in the sub menu.
 * @return {!Array<!goog.ui.MenuItem>} An array of menu items.
 * @deprecated Use getItemAt/getItemCount instead.
 */
goog.ui.SubMenu.prototype.getItems = function() {
  return this.getMenu().getItems();
};

/**
 * Gets a reference to the submenu's actual menu.
 * @return {!goog.ui.Menu} Reference to the object representing the sub menu.
 */
goog.ui.SubMenu.prototype.getMenu = function() {
  if (!this.subMenu_) {
    this.setMenu(
        new goog.ui.Menu(this.getDomHelper()), /* opt_internal */ true);
  } else if (this.externalSubMenu_ && this.subMenu_.getParent() != this) {
    // Since it is possible for the same popup menu to be attached to multiple
    // submenus, we need to ensure that it has the correct parent event target.
    this.subMenu_.setParent(this);
  }
  // Always create the menu DOM, for backward compatibility.
  if (!this.subMenu_.getElement()) {
    this.subMenu_.createDom();
  }
  return this.subMenu_;
};

/**
 * Sets the submenu to a specific menu.
 * @param {goog.ui.Menu} menu The menu to show when this item is selected.
 * @param {boolean=} opt_internal Whether this menu is an "internal" menu, and
 *     should be disposed of when this object is disposed of.
 */
goog.ui.SubMenu.prototype.setMenu = function(menu, opt_internal) {
  var oldMenu = this.subMenu_;
  if (menu != oldMenu) {
    if (oldMenu) {
      this.dismissSubMenu();
      if (this.isInDocument()) {
        this.setMenuListenersEnabled_(oldMenu, false);
      }
    }

    this.subMenu_ = menu;
    this.externalSubMenu_ = !opt_internal;

    if (menu) {
      menu.setParent(this);
      // There's no need to dispatch a HIDE event during submenu construction.
      menu.setVisible(false, /* opt_force */ true);
      menu.setAllowAutoFocus(false);
      menu.setFocusable(false);
      if (this.isInDocument()) {
        this.setMenuListenersEnabled_(menu, true);
      }
    }
  }
};

/**
 * Returns true if the provided element is to be considered inside the menu for
 * purposes such as dismissing the menu on an event.  This is so submenus can
 * make use of elements outside their own DOM.
 * @param {Element} element The element to test for.
 * @return {boolean} Whether or not the provided element is contained.
 */
goog.ui.SubMenu.prototype.containsElement = function(element) {
  return this.getMenu().containsElement(element);
};

/**
 * @param {boolean} isAdjustable Whether this submenu is adjustable.
 */
goog.ui.SubMenu.prototype.setPositionAdjustable = function(isAdjustable) {
  this.isPositionAdjustable_ = !!isAdjustable;
};

/**
 * @return {boolean} Whether this submenu is adjustable.
 */
goog.ui.SubMenu.prototype.isPositionAdjustable = function() {
  return this.isPositionAdjustable_;
};

// Register a decorator factory function for goog.ui.SubMenus.
goog.ui.registry.setDecoratorByClassName(
    goog.getCssName('goog-submenu'),
    function() { return new goog.ui.SubMenu(null); });


text:
@interface UIImageView (HMJHead)
- (void)hmj_setHeader:(NSString *)url;
- (void)hmj_setCircleHeader:(NSString *)url;
- (void)hmj_setRectHeader:(NSString *)url;
@end


text:
'use strict';
/*eslint-env mocha*/
/*jshint expr: true*/
/*jshint multistr: true*/
/**
 *  Test functions that process objects and arrays
 */

var chai = require('chai');
var expect = chai.expect;
var assert = require('chai').assert;
var _ = require('lodash');
var jsdom = require("jsdom");
var getType = require('get-object-type');

// code to be tested
var util = require('../lib/util');

describe('Midden Recursors', function() {
  it('should exist', function(){
    expect(util.processNested).to.be.a('function', '#processNested() is not a function');
  });

  describe('#processNested() HTML output', function(){
    var arrayName = 'arrname';
    var arrayValue = [1,2,3];
    var arrayHtml = util.processNested('arrname', arrayValue);
    //console.log('arrayHtml', arrayHtml);
    var arrayDom = jsdom.jsdom(arrayHtml);

    it('contains a div.midden-child element', function(){
      var mainEl = arrayDom.querySelector("li.midden-child");
      expect(mainEl.textContent).to.exist;
    });

    it('contains array name (li>div>span)', function(){
      var nameEl = arrayDom.querySelector("li > div > span");
      expect(nameEl.textContent).to.equal(arrayName);
    });

    it('should have div.midden-nest', function(){
      var nestEl = arrayDom.querySelector("div.midden-nest");
      expect(nestEl.textContent).to.exist;
    });

    it('should list array length', function(){
      var emEl = arrayDom.querySelector("div.midden-element em");
      expect(emEl.textContent).to.equal('(Array, ' + arrayValue.length + ' elements)');
    });

    it('should not have a class of midden-parent on any arrayValue', function(){
      var parentEl = arrayDom.querySelector(".midden-child .midden-parent");
      expect(parentEl).to.not.exist;
    });
  });

  describe('#processNested() Array', function(){
    var arrayName = 'arrname';
    var arrayValue = ['value one', 'value two', 'value 3'];
    var arrayHtml = util.processNested('arrname', arrayValue);
    //console.log('arrayHtml', arrayHtml);
    var arrayDom = jsdom.jsdom(arrayHtml);

    it('should print all values for array', function(){
      // ul > li > div
      var valuesList = arrayDom.querySelectorAll('ul > li > div.midden-element');
      expect(valuesList.length).equal(arrayValue.length);
    });
  });

  describe('#processNested() Nested Array', function(){
    var arrayName = 'arrname';
    var arrayValue = [[1,2], [3,4], [5,6]];
    var arrayHtml = util.processNested('arrname', arrayValue);
    //console.log('arrayHtml', arrayHtml);
    var arrayDom = jsdom.jsdom(arrayHtml);

    it('should print all values for array', function(){
      // ul > li > div
      var valuesList = arrayDom.querySelectorAll('ul > li > div.midden-element');
      expect(valuesList.length).equal(9);
    });
  });

  describe('#processNested() Object', function(){
    var objName = 'testObject';
    var objInstance = {
      'property1': 'a property value',
      'property2': 'another property value',
      'arrayProp': [1,2,3],
      'myMethod' : function(){}
    };
    var objectHtml = util.processNested(objName, objInstance);
    //console.log('objectHtml', objectHtml);
    var objDom = jsdom.jsdom(objectHtml);

    it('should print object name', function(){
      var nameEl = objDom.querySelector("li > div > span");
      expect(nameEl.textContent).to.equal(objName);
    });

    it('should count number of properties', function(){
      var emEl = objDom.querySelector("div.midden-element em");
      expect(emEl.textContent).to.equal('(Object, 4 properties)');
    });

    it('should print all properties for object', function(){
      // ul > li > div
      var valuesList = objDom.querySelectorAll('ul > li > div.midden-element');
      expect(valuesList.length).equal(7);
    });

    it('should have a class of midden-parent for arrayProp', function(){
      var parentEl = objDom.querySelector(".midden-parent");
      expect(parentEl).to.exist;
    });
  });

});


text:
package cn.jsprun.domain;
public class Forumlinks  implements java.io.Serializable {
	private static final long serialVersionUID = -4178195197402292593L;
	private Short id;
     private Short displayorder;
     private String name;
     private String url;
     private String description;
     private String logo;
    public Forumlinks() {
    }
    public Forumlinks(Short displayorder, String name, String url, String description, String logo) {
        this.displayorder = displayorder;
        this.name = name;
        this.url = url;
        this.description = description;
        this.logo = logo;
    }
    public Short getId() {
        return this.id;
    }
    public void setId(Short id) {
        this.id = id;
    }
    public Short getDisplayorder() {
        return this.displayorder;
    }
    public void setDisplayorder(Short displayorder) {
        this.displayorder = displayorder;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getUrl() {
        return this.url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getDescription() {
        return this.description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getLogo() {
        return this.logo;
    }
    public void setLogo(String logo) {
        this.logo = logo;
    }
}


text:
<resources>
    <string name="app_name">Daydream</string>
	<string name="random_quote_url_json">http://www.iheartquotes.com/api/v1/random?format=json&source=simpsons_chalkboard</string>
	<string name="volley_error">\"Oh no!\" - Mr. Bill\nSomething went wrong and we\'re not able to load a quote. Check your network connection.</string>
</resources>