text:
package org.innovateuk.ifs.management.competition.setup;

import org.innovateuk.ifs.commons.rest.RestResult;
import org.innovateuk.ifs.commons.security.SecuredBySpring;
import org.innovateuk.ifs.commons.service.ServiceResult;
import org.innovateuk.ifs.competition.resource.CompetitionResource;
import org.innovateuk.ifs.competition.resource.FundingRules;
import org.innovateuk.ifs.competition.service.CompetitionRestService;
import org.innovateuk.ifs.competition.service.CompetitionSetupRestService;
import org.innovateuk.ifs.competition.service.CompetitionThirdPartyConfigRestService;
import org.innovateuk.ifs.competition.service.TermsAndConditionsRestService;
import org.innovateuk.ifs.controller.ValidationHandler;
import org.innovateuk.ifs.file.resource.FileEntryResource;
import org.innovateuk.ifs.management.competition.setup.core.form.TermsAndConditionsForm;
import org.innovateuk.ifs.management.competition.setup.core.populator.TermsAndConditionsFormPopulator;
import org.innovateuk.ifs.management.competition.setup.core.populator.TermsAndConditionsModelPopulator;
import org.innovateuk.ifs.management.competition.setup.core.service.CompetitionSetupService;
import org.innovateuk.ifs.user.resource.UserResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.function.Supplier;

import static java.lang.String.format;
import static org.innovateuk.ifs.commons.rest.RestFailure.error;
import static org.innovateuk.ifs.commons.service.ServiceResult.serviceSuccess;
import static org.innovateuk.ifs.competition.resource.CompetitionSetupSection.TERMS_AND_CONDITIONS;
import static org.innovateuk.ifs.controller.ErrorToObjectErrorConverterFactory.*;
import static org.innovateuk.ifs.controller.FileUploadControllerUtils.getMultipartFileBytes;

@Controller
@RequestMapping("/competition/setup")
@SecuredBySpring(value = "Controller", description = "TODO", securedType = CompetitionSetupTermsAndConditionsController.class)
@PreAuthorize("hasAnyAuthority('comp_admin', 'project_finance')")
public class CompetitionSetupTermsAndConditionsController {
    public static final String COMPETITION_ID_KEY = "competitionId";
    public static final String COMPETITION_SETUP_FORM_KEY = "competitionSetupForm";
    private static final String MODEL = "model";
    private static final String PROJECT_COST_GUIDANCE_HTTP = "http://";

    @Autowired
    private CompetitionRestService competitionRestService;

    @Autowired
    private CompetitionSetupRestService competitionSetupRestService;

    @Autowired
    private TermsAndConditionsFormPopulator termsAndConditionsFormPopulator;

    @Autowired
    private TermsAndConditionsModelPopulator termsAndConditionsModelPopulator;

    @Autowired
    private TermsAndConditionsRestService termsAndConditionsRestService;

    @Autowired
    private CompetitionSetupService competitionSetupService;

    @Autowired
    private CompetitionThirdPartyConfigRestService competitionThirdPartyConfigRestService;

    @Value("${ifs.subsidy.control.northern.ireland.enabled:false}")
    private boolean subsidyControlNorthernIrelandEnabled;

    @GetMapping("/{competitionId}/section/terms-and-conditions")
    public String editTermsAndConditions(@PathVariable(COMPETITION_ID_KEY) long competitionId,
                                         Model model,
                                         UserResource loggedInUser) {
        CompetitionResource competition = competitionRestService.getCompetitionById(competitionId).getSuccess();

        if (!competitionSetupService.hasInitialDetailsBeenPreviouslySubmitted(competitionId)) {
            return ifsCompetitionSetup(competitionId);
        }

        if (competition.isNonIfs()) {
            return nonIfsCompetitionSetup(competitionId);
        }

        model.addAttribute(MODEL, termsAndConditionsModelPopulator.populateModel(competition, loggedInUser, false));
        model.addAttribute(COMPETITION_SETUP_FORM_KEY, termsAndConditionsFormPopulator.populateForm(competition));

        return "competition/setup";
    }

    @GetMapping("/{competitionId}/section/state-aid-terms-and-conditions")
    public String editStateAidTermsAndConditions(@PathVariable(COMPETITION_ID_KEY) long competitionId,
                                                 Model model,
                                                 UserResource loggedInUser) {
        CompetitionResource competition = competitionRestService.getCompetitionById(competitionId).getSuccess();

        if (!competitionSetupService.hasInitialDetailsBeenPreviouslySubmitted(competitionId)) {
            return ifsCompetitionSetup(competitionId);
        }

        if (competition.isNonIfs()) {
            return nonIfsCompetitionSetup(competitionId);
        }

        if (!shouldHaveSeparateTerms(competition)) {
            return ifsCompetitionSetup(competitionId);
        }

        model.addAttribute(MODEL, termsAndConditionsModelPopulator.populateModel(competition, loggedInUser, true));
        model.addAttribute(COMPETITION_SETUP_FORM_KEY, termsAndConditionsFormPopulator.populateFormForStateAid(competition));

        return "competition/setup";
    }

    @PostMapping("/{competitionId}/section/terms-and-conditions")
    public String submitTermsAndConditionsSectionDetails(@Valid @ModelAttribute(COMPETITION_SETUP_FORM_KEY) TermsAndConditionsForm termsAndConditionsForm,
                                                         @SuppressWarnings("UnusedParameters") BindingResult bindingResult,
                                                         ValidationHandler validationHandler,
                                                         @PathVariable(COMPETITION_ID_KEY) long competitionId,
                                                         UserResource loggedInUser,
                                                         Model model) {

        CompetitionResource competition = competitionRestService.getCompetitionById(competitionId).getSuccess();

        if (validationHandler.hasErrors()) {
            model.addAttribute(MODEL, termsAndConditionsModelPopulator.populateModel(competition, loggedInUser, false));
            return "competition/setup";
        }

        boolean isProcurementThirdParty = isProcurementThirdParty(termsAndConditionsForm.getTermsAndConditionsId());
        boolean isProcurement = isProcurement(termsAndConditionsForm.getTermsAndConditionsId());
        boolean isCompDataDeleted = false;

        if (isProcurementThirdParty) {
            termsAndConditionsFormPopulator.populateThirdPartyConfigData(termsAndConditionsForm, competition);
            validateThirdPartyConfigFields(competition, bindingResult);
            if (validationHandler.hasErrors()) {
                model.addAttribute(MODEL, termsAndConditionsModelPopulator.populateModel(competition, loggedInUser, false));
                return "competition/setup";
            }
            isCompDataDeleted = deleteDataInTermsSetupswitch(isProcurementThirdParty, isProcurement, competition, bindingResult);
        }

        if (isProcurement) {
            isCompDataDeleted = validateUploadFragment(isProcurementThirdParty, isProcurement, competition, bindingResult);
        }

        if (validationHandler.hasErrors()) {
            model.addAttribute(MODEL, termsAndConditionsModelPopulator.populateModel(isCompDataDeleted ? competitionRestService.getCompetitionById(competition.getId()).getSuccess() : competition, loggedInUser, false));
            return "competition/setup";
        }

        if ( !(isProcurement || isProcurementThirdParty)) {
            competitionSetupRestService.deleteCompetitionTerms(competitionId);
        }

        Supplier<ServiceResult<Void>> saveAction = () -> nonStateAidSaveAction(competition, termsAndConditionsForm);
        Supplier<String> postSaveRedirect = () -> postSaveRedirectForSingleTermsAndConditions(competition);

        if (isProcurementThirdParty(termsAndConditionsForm.getTermsAndConditionsId())) {
            saveThirdPartyTermsAndConditionsConfigData(isCompDataDeleted ? competitionRestService.getCompetitionById(competition.getId()).getSuccess() : competition);
        }
        return termsAndConditionsSection(validationHandler, isCompDataDeleted ? competitionRestService.getCompetitionById(competition.getId()).getSuccess() : competition, loggedInUser, model, saveAction, postSaveRedirect, false);
    }

    @PostMapping("/{competitionId}/section/state-aid-terms-and-conditions")
    public String submitStateAidTermsAndConditionsSectionDetails(@Valid @ModelAttribute(COMPETITION_SETUP_FORM_KEY) TermsAndConditionsForm termsAndConditionsForm,
                                                                 @SuppressWarnings("UnusedParameters") BindingResult bindingResult,
                                                                 ValidationHandler validationHandler,
                                                                 @PathVariable(COMPETITION_ID_KEY) long competitionId,
                                                                 UserResource loggedInUser,
                                                                 Model model) {
        CompetitionResource competition = competitionRestService.getCompetitionById(competitionId).getSuccess();

        if (!shouldHaveSeparateTerms(competition)) {
            return ifsCompetitionSetup(competition.getId());
        }

        Supplier<ServiceResult<Void>> saveAction = () -> stateAidSaveAction(competition, termsAndConditionsForm);
        Supplier<String> postSaveRedirect = () -> format("redirect:/competition/setup/%d/section/terms-and-conditions", competition.getId());
        return termsAndConditionsSection(validationHandler, competition, loggedInUser, model, saveAction, postSaveRedirect, true);
    }

    @PostMapping(path = "/{competitionId}/section/terms-and-conditions", params = "uploadTermsAndConditionsDoc")
    public String uploadTermsAndConditions(@Valid @ModelAttribute(COMPETITION_SETUP_FORM_KEY) TermsAndConditionsForm termsAndConditionsForm,
                                           @SuppressWarnings("UnusedParameters") BindingResult bindingResult,
                                           ValidationHandler validationHandler,
                                           @PathVariable(COMPETITION_ID_KEY) long competitionId,
                                           UserResource loggedInUser,
                                           Model model) {

        CompetitionResource competition = competitionRestService.getCompetitionById(competitionId).getSuccess();

        Supplier<ServiceResult<Void>> saveAction = () -> nonStateAidSaveAction(competition, termsAndConditionsForm);
        Supplier<String> postSaveRedirect = () -> postSaveRedirectForSingleTermsAndConditions(competition);
        Supplier<String> failure = () -> termsAndConditionsSection(validationHandler, competition, loggedInUser, model, saveAction, postSaveRedirect, false);

        MultipartFile file = getTermsAndConditionsFile(termsAndConditionsForm);

        boolean isProcurementThirdParty = isProcurementThirdParty(termsAndConditionsForm.getTermsAndConditionsId());
        boolean isProcurement = isProcurement(termsAndConditionsForm.getTermsAndConditionsId());
        boolean isCompDataDeleted = deleteDataInTermsSetupswitch(isProcurementThirdParty, isProcurement, competition, bindingResult);

        RestResult<FileEntryResource> uploadResult = competitionSetupRestService.uploadCompetitionTerms(competitionId, file.getContentType(), file.getSize(),
                file.getOriginalFilename(), getMultipartFileBytes(file));

        termsAndConditionsForm.setMarkAsCompleteAction(false);
        saveTermsAndConditions(isCompDataDeleted ? competitionRestService.getCompetitionById(competition.getId()).getSuccess(): competition , termsAndConditionsForm);

        Supplier<String> success = () -> format("redirect:/competition/setup/%d/section/terms-and-conditions", +competition.getId());
        return validationHandler.addAnyErrors(error(uploadResult.getErrors()), fileUploadField(getFileUploadedString(isCompDataDeleted ? competitionRestService.getCompetitionById(competition.getId()).getSuccess() :competition )), defaultConverters())
                .failNowOrSucceedWith(failure, success);
    }

    @PostMapping(path = "/{competitionId}/section/terms-and-conditions", params = "deleteTermsAndConditionsDoc")
    public String deleteTermsAndConditions(@Valid @ModelAttribute(COMPETITION_SETUP_FORM_KEY) TermsAndConditionsForm termsAndConditionsForm,
                                           @SuppressWarnings("UnusedParameters") BindingResult bindingResult,
                                           ValidationHandler validationHandler,
                                           @PathVariable(COMPETITION_ID_KEY) long competitionId,
                                           Model model) {
        CompetitionResource competition = competitionRestService.getCompetitionById(competitionId).getSuccess();
        Supplier<String> failureAndSuccessView = () -> format("redirect:/competition/setup/%d/section/terms-and-conditions", +competition.getId());

        RestResult<Void> deleteResult = competitionSetupRestService.deleteCompetitionTerms(competitionId);
        return validationHandler.addAnyErrors(error(deleteResult.getErrors()), fileUploadField(getFileUploadedString(competition)), defaultConverters())
                .failNowOrSucceedWith(failureAndSuccessView, failureAndSuccessView);
    }

    private String termsAndConditionsSection(ValidationHandler validationHandler,
                                             CompetitionResource competition,
                                             UserResource loggedInUser,
                                             Model model,
                                             Supplier<ServiceResult<Void>> saveAction,
                                             Supplier<String> postSaveRedirect,
                                             boolean stateAidPage) {
        if (!competitionSetupService.hasInitialDetailsBeenPreviouslySubmitted(competition.getId())) {
            return ifsCompetitionSetup(competition.getId());
        }

        if (competition.isNonIfs()) {
            return nonIfsCompetitionSetup(competition.getId());
        }

        Supplier<String> failureView = () -> {
            model.addAttribute(MODEL, termsAndConditionsModelPopulator.populateModel(competition, loggedInUser, stateAidPage));
            return "competition/setup";
        };

        return validationHandler.failNowOrSucceedWith(failureView, () -> {
            ServiceResult<Void> saveResult = saveAction.get();
            return validationHandler.addAnyErrors(saveResult, fieldErrorsToFieldErrors(), asGlobalErrors())
                    .failNowOrSucceedWith(failureView, postSaveRedirect);
        });
    }

    private ServiceResult<Void> stateAidSaveAction(CompetitionResource competition, TermsAndConditionsForm termsAndConditionsForm) {
        return saveOtherFundingRulesTermsAndConditions(competition, termsAndConditionsForm)
                .andOnSuccess(() -> optionallyMarkTermsSectionComplete(competition, termsAndConditionsForm));
    }

    private ServiceResult<Void> nonStateAidSaveAction(CompetitionResource competition, TermsAndConditionsForm termsAndConditionsForm) {
        if (shouldHaveSeparateTerms(competition)) {
            return saveTermsAndConditions(competition, termsAndConditionsForm);
        }
        return saveTermsAndConditions(competition, termsAndConditionsForm)
                .andOnSuccess(() -> optionallyMarkTermsSectionComplete(competition, termsAndConditionsForm));
    }

    private ServiceResult<Void> saveTermsAndConditions(CompetitionResource competition, TermsAndConditionsForm termsAndConditionsForm) {
        return competitionRestService.updateTermsAndConditionsForCompetition(
                competition.getId(),
                termsAndConditionsForm.getTermsAndConditionsId()
        ).toServiceResult();
    }

    private ServiceResult<Void> saveOtherFundingRulesTermsAndConditions(CompetitionResource competition, TermsAndConditionsForm termsAndConditionsForm) {
        return competitionRestService.updateOtherFundingRulesTermsAndConditionsForCompetition(
                competition.getId(),
                termsAndConditionsForm.getTermsAndConditionsId()
        ).toServiceResult();
    }

    private ServiceResult<Void> optionallyMarkTermsSectionComplete(CompetitionResource competition, TermsAndConditionsForm termsAndConditionsForm) {
        if (termsAndConditionsForm.isMarkAsCompleteAction()) {
            return competitionSetupRestService.markSectionComplete(competition.getId(), TERMS_AND_CONDITIONS).toServiceResult();
        }
        return serviceSuccess();
    }

    private boolean isProcurement(long termsAndConditionsId) {
        return termsAndConditionsRestService.getById(termsAndConditionsId).getSuccess().isProcurement();
    }

    private boolean shouldHaveSeparateTerms(CompetitionResource competition) {
        return subsidyControlNorthernIrelandEnabled
                && FundingRules.SUBSIDY_CONTROL == competition.getFundingRules()
                && !competition.isExpressionOfInterest();
    }

    private String ifsCompetitionSetup(long competitionId) {
        return "redirect:/competition/setup/" + competitionId;
    }

    private String nonIfsCompetitionSetup(long competitionId) {
        return "redirect:/non-ifs-competition/setup/" + competitionId;
    }

    private String postSaveRedirectForSingleTermsAndConditions(CompetitionResource competition) {
        if (shouldHaveSeparateTerms(competition)) {
            return format("redirect:/competition/setup/%d/section/state-aid-terms-and-conditions", competition.getId());
        }
        return format("redirect:/competition/setup/%d/section/%s", competition.getId(), TERMS_AND_CONDITIONS.getPostMarkCompletePath());
    }

    private boolean isProcurementThirdParty(long termsAndConditionsId) {
        return termsAndConditionsRestService.getById(termsAndConditionsId).getSuccess().isProcurementThirdParty();
    }

    private ServiceResult<Void> saveThirdPartyTermsAndConditionsConfigData(CompetitionResource competition) {
        if (competition.getCompetitionThirdPartyConfigResource().getId() == null) {
            competitionThirdPartyConfigRestService.create(competition.getCompetitionThirdPartyConfigResource());
        } else {
            competitionThirdPartyConfigRestService.update(competition.getId(), competition.getCompetitionThirdPartyConfigResource());
        }

        return serviceSuccess();
    }

    private void validateThirdPartyConfigFields(CompetitionResource competition, BindingResult bindingResult) {
        if (competition.getCompetitionThirdPartyConfigResource() == null) {
            bindingResult.addError(new FieldError(COMPETITION_SETUP_FORM_KEY, "thirdPartyTermsAndConditionsLabel", "Please enter a label to replace terms and conditions."));
            bindingResult.addError(new FieldError(COMPETITION_SETUP_FORM_KEY, "thirdPartyTermsAndConditionsText", "Please enter a description text for terms and conditions page."));
        } else {
            if (competition.getCompetitionThirdPartyConfigResource().getTermsAndConditionsLabel().isEmpty()) {
                bindingResult.addError(new FieldError(COMPETITION_SETUP_FORM_KEY, "thirdPartyTermsAndConditionsLabel", "Please enter a label to replace terms and conditions."));
            }
            if (competition.getCompetitionThirdPartyConfigResource().getTermsAndConditionsGuidance().isEmpty()) {
                bindingResult.addError(new FieldError(COMPETITION_SETUP_FORM_KEY, "thirdPartyTermsAndConditionsText", "Please enter a description text for terms and conditions page."));
            }
        }
        validateProjectCostGuidanceLink(competition, bindingResult);
    }

    private boolean validateUploadFragment(boolean isProcurementThirdParty, boolean isProcurement, CompetitionResource competition, BindingResult bindingResult) {
        boolean isCompDataDeleted = deleteDataInTermsSetupswitch(isProcurementThirdParty, isProcurement, competition, bindingResult);
        competition = isCompDataDeleted ? competitionRestService.getCompetitionById(competition.getId()).getSuccess() : competition;
        if (competition.getCompetitionTerms() == null) {
            if (isProcurement) {
                bindingResult.addError(new FieldError(COMPETITION_SETUP_FORM_KEY, "termsAndConditionsDoc", "Upload a terms and conditions document."));
            }
        }
        return isCompDataDeleted;
    }

    private boolean deleteDataInTermsSetupswitch(boolean isProcurementThirdParty, boolean isProcurement, CompetitionResource competition, BindingResult bindingResult) {
        boolean isCompDataDeleted = false;

        if (isProcurement) {
            if (competition.getCompetitionThirdPartyConfigResource() != null) {
                competitionSetupRestService.deleteCompetitionThirdPartyConfigData(competition.getId());
                isCompDataDeleted = true;
            }
            if (competition.getCompetitionTerms() != null && competition.getTermsAndConditions() != null && competition.getTermsAndConditions().isProcurementThirdParty()) {
                competitionSetupRestService.deleteCompetitionTerms(competition.getId());
                isCompDataDeleted = true;
            }
        }

        if (isProcurementThirdParty && !bindingResult.hasErrors()) {
            if (competition.getTermsAndConditions() != null && competition.getTermsAndConditions().isProcurement()) {
                competitionSetupRestService.deleteCompetitionTerms(competition.getId());
                saveThirdPartyTermsAndConditionsConfigData(competition);
                isCompDataDeleted = true;
            }
        }
        return  isCompDataDeleted;
    }

    private MultipartFile getTermsAndConditionsFile(TermsAndConditionsForm termsAndConditionsForm) {
        MultipartFile file = null;
        if (isProcurement(termsAndConditionsForm.getTermsAndConditionsId())) {
            file = termsAndConditionsForm.getTermsAndConditionsDoc();
        }
        if (isProcurementThirdParty(termsAndConditionsForm.getTermsAndConditionsId())) {
            file = termsAndConditionsForm.getThirdPartyTermsAndConditionsDoc();
        }
        return file;
    }

    private String getFileUploadedString(CompetitionResource competitionResource) {
        return competitionResource.getTermsAndConditions().isProcurement() ? "termsAndConditionsDoc"
                : "thirdPartyTermsAndConditionsDoc";
    }

    private void validateProjectCostGuidanceLink(CompetitionResource competition, BindingResult bindingResult) {
        if (competition.getCompetitionThirdPartyConfigResource() == null ||
                competition.getCompetitionThirdPartyConfigResource().getProjectCostGuidanceUrl().isEmpty() ||
                competition.getCompetitionThirdPartyConfigResource().getProjectCostGuidanceUrl().equals(PROJECT_COST_GUIDANCE_HTTP)) {
            bindingResult.addError(new FieldError(COMPETITION_SETUP_FORM_KEY, "projectCostGuidanceLink", "Please enter a project costs guidance link."));
        }
    }
}


text:
using System.Threading.Tasks;
using System.Windows.Input;

namespace GeeksWithBlogsToMarkdown.Commands.Base
{
    public interface IAsyncCommand : IAsyncCommand<object>
    {
    }

    public interface IAsyncCommand<in T> : IRaiseCanExecuteChanged
    {
        ICommand Command { get; }

        bool CanExecute(object obj);

        Task ExecuteAsync(T obj);
    }
}

redpajama_set_name: RedPajamaGithub


text:
<?xml version="1.0" encoding="UTF-8"?>

<!--

 This file is part of the Sylius package.

 (c) Paweł Jędrzejewski

 For the full copyright and license information, please view the LICENSE
 file that was distributed with this source code.

-->

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services
                               http://symfony.com/schema/dic/services/services-1.0.xsd">

    <parameters>
        <parameter key="sylius.builder.product.class">Sylius\Bundle\ProductBundle\Builder\ProductBuilder</parameter>
        <parameter key="sylius.validator.product.unique.class">Sylius\Bundle\ProductBundle\Validator\ProductUniqueValidator</parameter>
    </parameters>

    <services>
        <service id="sylius.form.type.product" class="%sylius.form.type.product.class%">
            <argument>%sylius.model.product.class%</argument>
            <argument>%sylius.validation_group.product%</argument>
            <tag name="form.type" alias="sylius_product" />
        </service>

        <service id="sylius.builder.product" class="%sylius.builder.product.class%">
            <argument type="service" id="sylius.manager.product" />
            <argument type="service" id="sylius.repository.product" />
            <argument type="service" id="sylius.repository.property" />
            <argument type="service" id="sylius.repository.product_property" />
        </service>

        <service id="sylius.validator.product.unique" class="%sylius.validator.product.unique.class%">
            <argument type="service" id="sylius.repository.product" />
            <tag name="validator.constraint_validator" alias="sylius.validator.product.unique" />
        </service>
    </services>

</container>


text:
package org.conqat.engine.core.driver.util;

import org.conqat.engine.core.driver.declaration.DeclarationAttribute;
import org.conqat.engine.core.driver.specification.BlockSpecificationOutput;

/**
 * Interface for marking objects that reference generated data (outputs). As
 * these are either {@link BlockSpecificationOutput}s or
 * {@link DeclarationAttribute}s, there are two methods returning this object
 * casted to the corresponding class. Exacly one of these methods must return
 * null.
 *
 * @author $Author: kinnen $
 * @version $Rev: 41751 $
 * @ConQAT.Rating GREEN Hash: 31170064295A3C7F8C937406E8384425
 */
public interface IInputReferencer {

	/** Returns the object referenced from this one. */
	public IInputReferencable getReference();

	/**
	 * Returns this object cast to a {@link DeclarationAttribute} or
	 * null if this is not possible. If this returns
	 * null, then {@link #asBlockSpecificationOutput()} must return
	 * a non-null value.
	 */
	public DeclarationAttribute asDeclarationAttribute();

	/**
	 * Returns this object cast to a {@link BlockSpecificationOutput} or
	 * null if this is not possible. If this returns
	 * null, then {@link #asDeclarationAttribute()} must return a
	 * non-null value.
	 */
	public BlockSpecificationOutput asBlockSpecificationOutput();
}


text:
'use strict';

const constValue = module.exports;

// oss demo callback server an ecs, used with PutObject、PostObject、CompleteMultipartUpload
constValue.callbackServer = 'http://oss-demo.aliyuncs.com:23450';


text:
Dashboard

Google Map Settings

Google Map Settings

<mat-card class="material-card-sm">
        <alert [(displayed)]="objAlert.show" [type]="objAlert.type" [closeButton]="objAlert.closeButton">
            {{objAlert.title}}! {{objAlert.message}}
        </alert>

        <form role="form" novalidate [formGroup]="googleMapForm" (ngSubmit)="saveMapSettings()">

                        <mat-form-field class="material-input-full">
                            <input matInput placeholder="Api Key" formControlName="googleMapsApiKey" [(ngModel)]="objMap.googleMapsApiKey">
                            <mat-error>
                                <control-messages [isSubmitted]="isSubmitted" [control]="googleMapForm.controls['googleMapsApiKey']"></control-messages>
                            </mat-error>
                        </mat-form-field>

                        <mat-form-field class="material-input-full">
                            <input matInput placeholder="Place Name" formControlName="placeName" [(ngModel)]="objMap.placeName">
                            <mat-error>
                                <control-messages [isSubmitted]="isSubmitted" [control]="googleMapForm.controls['placeName']"></control-messages>
                            </mat-error>
                        </mat-form-field>

                        <mat-form-field class="material-input-full">
                            <input matInput placeholder="Longitude" type="number" formControlName="longitude" [(ngModel)]="objMap.longitude">
                            <mat-error>
                                <control-messages [isSubmitted]="isSubmitted" [control]="googleMapForm.controls['longitude']"></control-messages>
                            </mat-error>
                        </mat-form-field>

                        <mat-form-field class="material-input-full">
                            <input matInput placeholder="Latitude" type="number" formControlName="latitude" [(ngModel)]="objMap.latitude">
                            <mat-error>
                                <control-messages [isSubmitted]="isSubmitted" [control]="googleMapForm.controls['latitude']"></control-messages>
                            </mat-error>
                        </mat-form-field>

                        <mat-form-field class="material-input-full">
                            <input matInput placeholder="Zoom Level" type="number" formControlName="zoom" [(ngModel)]="objMap.zoom" min="1" max="20">
                            <mat-error>
                                    <control-messages [isSubmitted]="isSubmitted" [control]="googleMapForm.controls['zoom']"></control-messages>
                            </mat-error>
                        </mat-form-field>

                        <mat-form-field class="full-width">
                                <mat-select class="full-width" placeholder="Map Type" formControlName="mapType" [(ngModel)]="objMap.mapType">
                                  <mat-option value="">Select Map Type</mat-option>
                                  <mat-option *ngFor="let type of mapTypes" [value]="type">
                                        {{type}}
                                  </mat-option>
                                </mat-select>
                        </mat-form-field>

                        <mat-form-field class="material-input-full">
                            <input matInput placeholder="Marker Title" formControlName="markerTitle" [(ngModel)]="objMap.markerTitle">
                        </mat-form-field>

                        <mat-checkbox [checked]="objMap.scrollWheel" formControlName="scrollWheel"[(ngModel)]="objMap.scrollWheel">
                            Scroll Wheel
                        </mat-checkbox>

                        <mat-checkbox [checked]="objMap.showMarker" formControlName="showMarker"[(ngModel)]="objMap.showMarker">
                            Show Marker
                        </mat-checkbox>

                        <button mat-raised-button color="primary" type="submit">Save</button>

        </form>
</mat-card>


text:
@interface Coax : UIView
    @property (strong, nonatomic) IBOutlet UILabel *coax_label;
@end


text:
package com.mkyong.helloworld;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.junit.Test;

import com.mkyong.helloworld.domain.AbstractDomain;
import com.mkyong.helloworld.domain.BushoDomain;
import com.mkyong.helloworld.domain.KokyakuDomain;
import com.mkyong.helloworld.kubun.BushoKubun;
import com.mkyong.helloworld.kubun.i.Kubun;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.NotFoundException;
import junit.framework.TestCase;

public class TestSample extends TestCase {

	@Test
	public void test01() {
		Class<BushoDomain> clazz = BushoDomain.class;
		Constructor<BushoDomain> ctor;
		try {

			ctor = clazz.getConstructor();
			BushoDomain instance = ctor.newInstance();
			// System.out.println(instance.isEigyosho());

		} catch (NoSuchMethodException | SecurityException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void test02() {
		try {

			CtClass origCtClass = ClassPool.getDefault().get(BushoDomain.class.getName());
			CtClass emptyCtClass = ClassPool.getDefault().makeClass(origCtClass.getName() + "Empty", origCtClass);

			CtMethod m = CtNewMethod.make("public void madeMethod(String bar) { throw new RuntimeException(bar); }",
					emptyCtClass);
			emptyCtClass.addMethod(m);
			Class emptyClass = emptyCtClass.toClass();

			BushoDomain empty = (BushoDomain) emptyClass.newInstance();
			// System.out.println(empty.getClass().getName());
			// System.out.println(empty.isEigyosho());

			Method addedMethod = empty.getClass().getMethod("madeMethod", String.class);

		} catch (CannotCompileException e) {
			e.printStackTrace();
		} catch (NotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}

	}

	@Test
	public void test03() {
		Class originalClass = BushoDomain.class;
		Method[] methods = originalClass.getDeclaredMethods();
		for (Method method : methods) {
			// System.out.println(method.getName() + " :=> " +
			// method.getReturnType());
			Class<?> returnType = method.getReturnType();
			if (returnType.isAssignableFrom(BushoKubun.class)) {
				// System.out.println("BushoKubun が 返却の型だよ!!");
			}
			if (returnType.isAssignableFrom(boolean.class)) {
				// System.out.println("boolean が 返却の型だよ!!!!");
			}
		}
	}

	@Test
	public void test04() throws Exception {

		ClassPool classPool = ClassPool.getDefault();
		CtClass origCtClass = classPool.get(KokyakuDomain.class.getName());
		CtClass emptyCtClass = classPool.makeClass(origCtClass.getName() + "Empty", origCtClass);

		CtMethod[] origMethods = origCtClass.getDeclaredMethods();
		CtMethod[] emptyMethods = emptyCtClass.getMethods();

		// exclude superclass method from empty method
		// TODO

		for (CtMethod method : emptyMethods) {

			System.out.println("---");
			System.out.println(method.getName());

			CtClass returnType = method.getReturnType();

			if (returnType.equals(CtClass.voidType)) {
				method.setBody(";");
			} else if (returnType.equals(CtClass.booleanType)) {
				method.setBody("return false;");
			}

			boolean isDomain = returnType.subclassOf(classPool.get(AbstractDomain.class.getName()));
			if (isDomain) {
			}

			CtClass[] interfaces = returnType.getInterfaces();
			for (CtClass _interface : interfaces) {
				boolean isKubun = _interface.equals(classPool.get(Kubun.class.getName()));
				if (isKubun) {
					method.setBody("return null;");
				}
			}
		}
	}

}


text:
<?php namespace App\Events;

abstract class Event {

	//

}


text:
var module = require('./_module_init.js');
module.factory('$QJLogger', [
	'$rootScope', '$state', '$timeout',
	function($rootScope, $state, $timeout) {
		return {
			log: function(msg) {
				var appName = $rootScope.config.appName;
				console.info('[' + appName + '][' + msg + ']');
			}
		}
	}
]);


text:
#region Usings

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using NUnit.Framework;

#endregion

namespace FSOManagement.Tests.Util
{
    public static class SerializationAssert
    {
        public static void IsSerializable<T>(T obj)
        {
            if (!typeof(T).IsSerializable)
            {
                Assert.Fail("Type if not marked as serializable!");
            }
            else
            {
                var formatter = new BinaryFormatter();

                using (var stream = new MemoryStream())
                {
                    Assert.DoesNotThrow(() => formatter.Serialize(stream, obj));
                }
            }
        }
    }
}


text:
package com.jivesoftware.os.rcvs.hbase098;

import com.jivesoftware.os.mlogger.core.MetricLogger;
import com.jivesoftware.os.mlogger.core.MetricLoggerFactory;
import java.io.IOException;
import java.util.Arrays;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableExistsException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

/**
 * Provides the meta data information about an HBase table.
 */
public class HBaseTableConfiguration {

    private static final MetricLogger LOG = MetricLoggerFactory.getLogger(true);

    private static final String NAMESPACE_AND_NAME_DELIM = ".";

    private final Configuration configuration;
    private final String tableNameSpace;
    private final String tableName;
    private final String columnFamilyName;
    private final String[] columnFamilyNames;
    private final int timeToLiveInSeconds;
    private final int minVersions;
    private final int maxVersions;

    public HBaseTableConfiguration(Configuration configuration,
        String tableNameSpace,
        String tableName,
        String columnFamilyName) {
        this(configuration, tableNameSpace, tableName, new String[] { columnFamilyName });
    }

    public HBaseTableConfiguration(Configuration configuration,
        String tableNameSpace,
        String tableName,
        String[] columnFamilyNames) {
        this(configuration, tableNameSpace, tableName, columnFamilyNames, -1, -1, -1);
    }

    public HBaseTableConfiguration(Configuration configuration,
        String tableNameSpace,
        String tableName,
        String columnFamilyName,
        int ttlInSeconds,
        int minVersions,
        int maxVersions) {
        this(configuration, tableNameSpace, tableName, new String[] { columnFamilyName }, ttlInSeconds, minVersions, maxVersions);
    }

    public HBaseTableConfiguration(Configuration configuration,
        String tableNameSpace,
        String tableName,
        String[] columnFamilyNames,
        int ttlInSeconds,
        int minVersions,
        int maxVersions) {
        if (configuration == null) {
            this.configuration = HBaseConfiguration.create();
        } else {
            this.configuration = configuration;
        }
        this.tableNameSpace = tableNameSpace.trim();
        this.tableName = tableName;
        this.columnFamilyName = columnFamilyNames[0];
        this.columnFamilyNames = columnFamilyNames;
        this.timeToLiveInSeconds = ttlInSeconds;
        this.minVersions = minVersions;
        this.maxVersions = maxVersions;

    }

    public byte[] ensureTableProvisioned(boolean createTable) throws IOException {
        return ensureTableProvisioned(createTable, false);
    }

    public byte[] ensureTableProvisioned(boolean createTable, boolean createColumnFamilies) throws IOException {
        HBaseAdmin admin = new HBaseAdmin(configuration);

        HTableDescriptor tableDescriptor = null;
        String finalName = getFinalName();
        try {
            tableDescriptor = admin.getTableDescriptor(finalName.getBytes());
        } catch (org.apache.hadoop.hbase.TableNotFoundException tnfe) {
            if (!createTable) {
                throw tnfe;
            }
        }
        if (tableDescriptor == null) {
            tableDescriptor = new HTableDescriptor(finalName);
            for (String columnFamily : columnFamilyNames) {
                HColumnDescriptor hColumnDescriptor = buildHColumnDescriptor(columnFamily);
                tableDescriptor.addFamily(hColumnDescriptor);
            }
            try {
                admin.createTable(tableDescriptor);
                LOG.info("Created table: " + finalName + " with column family names: " + Arrays.toString(columnFamilyNames));
            } catch (TableExistsException tee) {
                LOG.info("Cannot create table since it already exists: " + finalName + " with column family names: "
                    + Arrays.toString(columnFamilyNames), tee);
            }
        } else {
            for (String columnFamily : columnFamilyNames) {
                HColumnDescriptor column = tableDescriptor.getFamily(columnFamily.getBytes());
                if (column == null) {
                    if (createColumnFamilies) {
                        try {
                            HColumnDescriptor hColumnDescriptor = buildHColumnDescriptor(columnFamily);
                            if (admin.isTableEnabled(finalName)) {
                                admin.disableTable(finalName);
                            }
                            admin.addColumn(finalName, hColumnDescriptor);
                        } finally {
                            admin.enableTable(finalName);
                        }
                    } else {
                        LOG.error("Table '" + finalName + "' exists, but expected column family name '" + columnFamilyName + "' does not");
                        throw new IOException("Table '" + finalName + "' exists, but expected column family name '" + columnFamilyName + "' does not");
                    }
                }
            }
        }

        return finalName.getBytes();
    }

    private HColumnDescriptor buildHColumnDescriptor(String columnFamily) {
        HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(columnFamily);
        if (timeToLiveInSeconds > -1) {
            hColumnDescriptor.setTimeToLive(timeToLiveInSeconds);
        }
        if (maxVersions > -1) {
            hColumnDescriptor.setMaxVersions(maxVersions);
        }
        if (minVersions > -1) {
            hColumnDescriptor.setMinVersions(minVersions);
        }
        return hColumnDescriptor;
    }

    public String getTableNameSpace() {
        return tableNameSpace;
    }

    public String getUnprefixedTableName() {
        return tableName;
    }

    /**
     * Provides the full name to use when referencing the table in HBase which is a combination of the namespace
     * (if it exists) and the table name.
     *
     * @return name of table as HBase sees it.
     */
    public String getFinalName() {
        if (tableNameSpace != null && tableNameSpace.length() > 0) {
            return tableNameSpace + NAMESPACE_AND_NAME_DELIM + tableName;
        }

        return tableName;
    }

    public String getColumnFamilyName() {
        return columnFamilyName;
    }
}


text: Image pair matching game.


text:
namespace ui {
class Layer;
}

namespace ash {

// ScreenDimmer displays a partially-opaque layer above everything
// else in the root window to darken the display.  It shouldn't be used
// for long-term brightness adjustments due to performance
// considerations -- it's only intended for cases where we want to
// briefly dim the screen (e.g. to indicate to the user that we're
// about to suspend a machine that lacks an internal backlight that
// can be adjusted).
class ASH_EXPORT ScreenDimmer : public aura::WindowObserver {
 public:
  class TestApi {
   public:
    explicit TestApi(ScreenDimmer* dimmer) : dimmer_(dimmer) {}

    ui::Layer* layer() { return dimmer_->dimming_layer_.get(); }

   private:
    ScreenDimmer* dimmer_;  // not owned

    DISALLOW_COPY_AND_ASSIGN(TestApi);
  };

  explicit ScreenDimmer(aura::Window* root_window);
  ~ScreenDimmer() override;

  // Dim or undim the root window.
  void SetDimming(bool should_dim);

  // aura::WindowObserver overrides:
  void OnWindowBoundsChanged(aura::Window* root_window,
                             const gfx::Rect& old_bounds,
                             const gfx::Rect& new_bounds) override;

 private:
  friend class TestApi;

  aura::Window* root_window_;

  // Partially-opaque layer that's stacked above all of the root window's
  // children and used to dim the screen.  NULL until the first time we dim.
  scoped_ptr<ui::Layer> dimming_layer_;

  // Are we currently dimming the screen?
  bool currently_dimming_;

  DISALLOW_COPY_AND_ASSIGN(ScreenDimmer);
};

}  // namespace ash

#endif  // ASH_WM_SCREEN_DIMMER_H_


text:
<?php include 'header.php'; ?>

<div id="body-wrapper" class="wrapper wrapper-content animated fadeInRight
<?php echo isset($article) && $article ? 'article' : ''; ?>">

</div>

<?php include 'scripts.php'; ?>

<!-- JI Framework -->
<script type="text/javascript">

    initJIFramework('<?php echo isset($js) ? $js : '';;?>', <?php echo isset($data) ? $data : '{}';  ?>);

</script>

<?php include 'footer.php'; ?>


text:
package com.nikhilw.adt.trees.family;

import java.util.HashSet;
import java.util.Set;

/**
 * Represents a relationship between two people. Any two people can have
 * multiple relationships, considering the scenario of re-marriage to the same
 * person. Equality is be object and not by value.
 *
 * @author nikhil
 */
public class Relation {
	private Person person1;
	private Person person2;
	private Set<Person> children;

	public Relation(Person person1, Person person2) {
		this.person1 = person1;
		this.person2 = person2;
		this.children = new HashSet<>();

		this.person1.addRelationship(this);
		this.person2.addRelationship(this);
	}

	public Person getPerson1() {
		return person1;
	}

	public Person getPerson2() {
		return person2;
	}

	public Set<Person> getChildren() {
		return children;
	}

	public void addChild(Person child) {
		children.add(child);
	}
}


text:
@implementation NSString (URLEncode)

- (NSString *)URLEncodedString
{
    NSString *result =
    (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                (CFStringRef)self,
                                                                                NULL,
                                                                                CFSTR("!*'();:@&=+$,/?%#[]"),
                                                                                kCFStringEncodingUTF8));
	return result;
}

- (NSString *)URLDecodedString
{
    NSString *result = (NSString *)CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
                                                                                (CFStringRef)self,
                                                                                CFSTR(""),
                                                                                kCFStringEncodingUTF8));
	return result;
}

@end


text:
#include "OptimizationTests_AutoFastJNIDetection_RecursiveCall_Main.h"

int foo(int a, int b) {
    if (a > 0) {
        return foo(a - 1, b + 5);
    } else {
      return b;
    }
}
// Test add
extern "C" JNIEXPORT jint JNICALL Java_OptimizationTests_AutoFastJNIDetection_RecursiveCall_Main_nativeRecursiveCall(JNIEnv *env, jobject myobject, jint a, jint b, jint c) {
    return foo(c, a + b);
}


text:
'use strict';

/**
 * Encode the given string through `encodeURIComponent`
 * and encode parenthesis as well.
 * This is needed to avoid an nginx error (method not allowed)
 */
module.exports = function encodeURIComponentAndParentheses(string) {
  string = encodeURIComponent(string);
  string = string.replace(/\(/g, "%28");
  string = string.replace(/\)/g, "%29");
  return string;
};


text:
package org.mockito.internal.listeners;

public interface MockingProgressListener {
}


text:
"""K-means clustering"""

# Authors: Gael Varoquaux <gael.varoquaux@normalesup.org>
#          Thomas Rueckstiess <ruecksti@in.tum.de>
#          James Bergstra <james.bergstra@umontreal.ca>
#          Jan Schlueter <scikit-learn@jan-schlueter.de>
#          Nelle Varoquaux
#          Peter Prettenhofer <peter.prettenhofer@gmail.com>
# License: BSD

import warnings
from itertools import cycle, izip

import numpy as np
import scipy.sparse as sp

from ..base import BaseEstimator
from ..metrics.pairwise import euclidean_distances
from ..utils import check_arrays
from ..utils import check_random_state
from ..utils import gen_even_slices
from ..utils import shuffle
from ..utils import warn_if_not_float

from . import _k_means

###############################################################################
# Initialisation heuristic

def k_init(X, k, n_local_trials=None, random_state=None, x_squared_norms=None):
    """Init k seeds according to kmeans++

    Parameters
    -----------
    X: array, shape (n_samples, n_features)
        The data to pick seeds for

    k: integer
        The number of seeds to choose

    n_local_trials: integer, optional
        The number of seeding trials for each center (except the first),
        of which the one reducing inertia the most is greedily chosen.
        Set to None to make the number of trials depend logarithmically
        on the number of seeds (2+log(k)); this is the default.

    random_state: numpy.RandomState, optional
        The generator used to initialize the centers. Defaults to numpy.random.

    x_squared_norms: array, shape (n_samples,), optional
        Squared euclidean norm of each data point. Pass it if you have it at
        hands already to avoid it being recomputed here. Default: None

    Notes
    ------
    Selects initial cluster centers for k-mean clustering in a smart way
    to speed up convergence. see: Arthur, D. and Vassilvitskii, S.
    "k-means++: the advantages of careful seeding". ACM-SIAM symposium
    on Discrete algorithms. 2007

    Version ported from http://www.stanford.edu/~darthur/kMeansppTest.zip,
    which is the implementation used in the aforementioned paper.
    """
    n_samples, n_features = X.shape
    random_state = check_random_state(random_state)

    centers = np.empty((k, n_features))

    # Set the number of local seeding trials if none is given
    if n_local_trials is None:
        # This is what Arthur/Vassilvitskii tried, but did not report
        # specific results for other than mentioning in the conclusion
        # that it helped.
        n_local_trials = 2 + int(np.log(k))

    # Pick first center randomly
    center_id = random_state.randint(n_samples)
    centers[0] = X[center_id]

    # Initialize list of closest distances and calculate current potential
    if x_squared_norms is None:
        x_squared_norms = X.copy()
        x_squared_norms **= 2
        x_squared_norms = x_squared_norms.sum(axis=1)
    closest_dist_sq = euclidean_distances(
        np.atleast_2d(centers[0]), X, Y_norm_squared=x_squared_norms,
        squared=True)
    current_pot = closest_dist_sq.sum()

    # Pick the remaining k-1 points
    for c in xrange(1, k):
        # Choose center candidates by sampling with probability proportional
        # to the squared distance to the closest existing center
        rand_vals = random_state.random_sample(n_local_trials) * current_pot
        candidate_ids = np.searchsorted(closest_dist_sq.cumsum(), rand_vals)

        # Compute distances to center candidates
        distance_to_candidates = euclidean_distances(
            X[candidate_ids], X, Y_norm_squared=x_squared_norms, squared=True)

        # Decide which candidate is the best
        best_candidate = None
        best_pot = None
        best_dist_sq = None
        for trial in xrange(n_local_trials):
            # Compute potential when including center candidate
            new_dist_sq = np.minimum(closest_dist_sq,
                                     distance_to_candidates[trial])
            new_pot = new_dist_sq.sum()

            # Store result if it is the best local trial so far
            if (best_candidate is None) or (new_pot < best_pot):
                best_candidate = candidate_ids[trial]
                best_pot = new_pot
                best_dist_sq = new_dist_sq

        # Permanently add best center candidate found in local tries
        centers[c] = X[best_candidate]
        current_pot = best_pot
        closest_dist_sq = best_dist_sq

    return centers

###############################################################################
# K-means estimation by EM (expectation maximisation)

def k_means(X, k, init='k-means++', n_init=10, max_iter=300, verbose=0,
                    tol=1e-4, random_state=None, copy_x=True):
    """ K-means clustering algorithm.

    Parameters
    ----------
    X: ndarray
        A M by N array of M observations in N dimensions or a length
        M array of M one-dimensional observations.

    k: int or ndarray
        The number of clusters to form as well as the number of
        centroids to generate.

    max_iter: int, optional, default 300
        Maximum number of iterations of the k-means algorithm to run.

    n_init: int, optional, default: 10
        Number of time the k-means algorithm will be run with different
        centroid seeds. The final results will be the best output of
        n_init consecutive runs in terms of inertia.

    init: {'k-means++', 'random', or ndarray, or a callable}, optional
        Method for initialization, default to 'k-means++':

        'k-means++' : selects initial cluster centers for k-mean
        clustering in a smart way to speed up convergence. See section
        Notes in k_init for more details.

        'random': generate k centroids from a Gaussian with mean and
        variance estimated from the data.

        If an ndarray is passed, it should be of shape (k, p) and gives
        the initial centers.

        If a callable is passed, it should take arguments X, k and
        and a random state and return an initialization.

    tol: float, optional
        The relative increment in the results before declaring convergence.

    verbose: boolean, optional
        Terbosity mode

    random_state: numpy.RandomState, optional
        The generator used to initialize the centers. Defaults to numpy.random.

    copy_x: boolean, optional
        When pre-computing distances it is more numerically accurate to center
        the data first.  If copy_x is True, then the original data is not
        modified.  If False, the original data is modified, and put back before
        the function returns, but small numerical differences may be introduced
        by subtracting and then adding the data mean.

    Returns
    -------
    centroid: ndarray
        A k by N array of centroids found at the last iteration of
        k-means.

    label: ndarray
        label[i] is the code or index of the centroid the
        i'th observation is closest to.

    inertia: float
        The final value of the inertia criterion

    """
    random_state = check_random_state(random_state)

    vdata = np.mean(np.var(X, 0))
    best_inertia = np.infty

    # subtract of mean of x for more accurate distance computations
    X_mean = X.mean(axis=0)
    if copy_x:
        X = X.copy()
    X -= X_mean

    if hasattr(init, '__array__'):
        init = np.asarray(init).copy()
        init -= X_mean
        if not n_init == 1:
            warnings.warn('Explicit initial center position passed: '
                          'performing only one init in the k-means')
            n_init = 1

    # precompute squared norms of data points
    x_squared_norms = X.copy()
    x_squared_norms **= 2
    x_squared_norms = x_squared_norms.sum(axis=1)
    for it in range(n_init):
        # init
        centers = _init_centroids(X, k, init, random_state=random_state,
                                  x_squared_norms=x_squared_norms)
        if verbose:
            print 'Initialization complete'

        # iterations
        for i in range(max_iter):
            centers_old = centers.copy()
            labels, inertia = _e_step(X, centers,
                                      x_squared_norms=x_squared_norms)
            centers = _m_step(X, labels, k)

            if verbose:
                print 'Iteration %i, inertia %s' % (i, inertia)

            if np.sum((centers_old - centers) ** 2) < tol * vdata:
                if verbose:
                    print 'Converged to similar centers at iteration', i
                break

            if inertia < best_inertia:
                best_labels = labels.copy()
                best_centers = centers.copy()
                best_inertia = inertia

    else:
        best_labels = labels
        best_centers = centers
        best_inertia = inertia
    if not copy_x:
        X += X_mean
    return best_centers + X_mean, best_labels, best_inertia

def _calculate_labels_inertia(X, centers, x_squared_norms=None):
    """Compute the inertia and the labels of the given samples and centers"""
    distance = euclidean_distances(centers, X, x_squared_norms, squared=True)
    return distance.min(axis=0).sum(), distance.argmin(axis=0)

def _m_step(X, z, k):
    """M step of the K-means EM algorithm

    Computation of cluster centers/means

    Parameters
    ----------
    X: array, shape (n_samples, n_features)

    z: array, shape (n_samples)
        Current assignment

    k: int
        Number of desired clusters

    Returns
    -------
    centers: array, shape (k, n_features)
        The resulting centers
    """
    dim = X.shape[1]
    centers = np.empty((k, dim))
    X_center = None
    for q in range(k):
        center_mask = (z == q)
        if not np.any(center_mask):
            # The centroid of empty clusters is set to the center of
            # everything
            if X_center is None:
                X_center = X.mean(axis=0)
            centers[q] = X_center
        else:
            centers[q] = np.mean(X[center_mask], axis=0)
    return centers

def _init_centroids(X, k, init, random_state=None, x_squared_norms=None):
    """Compute the initial centroids

    Parameters
    ----------

    X: array, shape (n_samples, n_features)

    k: int
        number of centroids

    init: {'k-means++', 'random' or ndarray or callable} optional
        Method for initialisation

    random_state: numpy.RandomState, optional
        The generator used to initialise the centers. Defaults to numpy.random

    x_squared_norms:  array, shape (n_samples,), optional
        Squared euclidean norm of each data point. Pass it if you have it at
        hands already to avoid it being recomputed here. Default: None

    Returns
    -------
    centers: array, shape(k, n_features)
    """
    random_state = check_random_state(random_state)

    n_samples = X.shape[0]
    if init == 'k-means++':
        if sp.issparse(X):
            raise ValueError("Init method 'k-means++' only for dense X.")
        centers = k_init(X, k,
                        random_state=random_state,
                        x_squared_norms=x_squared_norms)
    elif init == 'random':
        seeds = np.argsort(random_state.rand(n_samples))[:k]
        centers = X[seeds]
    elif hasattr(init, '__array__'):
        centers = init
    elif callable(init):
        centers = init(X, k, random_state=random_state)
    else:
        raise ValueError("the init parameter for the k-means should "
            "be 'k-means++' or 'random' or an ndarray, "
            "'%s' (type '%s') was passed.")

    if sp.issparse(centers):
        centers = centers.toarray()
    return centers

def _e_step(x, centers, precompute_distances=True, x_squared_norms=None):
    """E step of the K-means EM algorithm

    Computation of the input-to-cluster assignment

    Parameters
    ----------
    x: array, shape (n_samples, n_features)

    centers: array, shape (k, n_features)
        The cluster centers

    precompute_distances: bool, optional
        Whether to compute the full distance matrix between centers and data
        points at once for more speed at the cost of memory. Default: True

    x_squared_norms: array, shape (n_samples,), optional
        Squared euclidean norm of each data point, speeds up computations in
        case of precompute_distances == True. Default: None

    Returns
    -------
    z: array of shape(n)
        The resulting assignment

    inertia: float
        The value of the inertia criterion with the assignment
    """

    n_samples = x.shape[0]
    k = centers.shape[0]

    if precompute_distances:
        distances = euclidean_distances(centers, x, x_squared_norms,
                                        squared=True)
    z = np.empty(n_samples, dtype=np.int)
    z.fill(-1)
    mindist = np.empty(n_samples)
    mindist.fill(np.infty)
    for q in range(k):
        if precompute_distances:
            dist = distances[q]
        else:
            dist = np.sum((x - centers[q]) ** 2, axis=1)
        z[dist < mindist] = q
        mindist = np.minimum(dist, mindist)
    inertia = mindist.sum()
    return z, inertia

class KMeans(BaseEstimator):
    """K-Means clustering

    Parameters
    ----------

    k : int, optional, default: 8
        The number of clusters to form as well as the number of
        centroids to generate.

    max_iter : int
        Maximum number of iterations of the k-means algorithm for a
        single run.

    n_init: int, optional, default: 10
        Number of time the k-means algorithm will be run with different
        centroid seeds. The final results will be the best output of
        n_init consecutive runs in terms of inertia.

    init : {'k-means++', 'random' or an ndarray}
        Method for initialization, defaults to 'k-means++':

        'k-means++' : selects initial cluster centers for k-mean
        clustering in a smart way to speed up convergence. See section
        Notes in k_init for more details.

        'random': choose k observations (rows) at random from data for
        the initial centroids.

        if init is an 2d array, it is used as a seed for the centroids

    tol: float, optional default: 1e-4
        Relative tolerance w.r.t. inertia to declare convergence

    Methods
    -------

    fit(X):
        Compute K-Means clustering

    Attributes
    ----------

    cluster_centers_: array, [n_clusters, n_features]
        Coordinates of cluster centers

    labels_:
        Labels of each point

    inertia_: float
        The value of the inertia criterion associated with the chosen
        partition.

    Notes
    ------

    The k-means problem is solved using the Lloyd algorithm.

    The average complexity is given by O(k n T), were n is the number of
    samples and T is the number of iteration.

    The worst case complexity is given by O(n^(k+2/p)) with
    n = n_samples, p = n_features. (D. Arthur and S. Vassilvitskii,
    'How slow is the k-means method?' SoCG2006)

    In practice, the K-means algorithm is very fast (one of the fastest
    clustering algorithms available), but it falls in local minima. That's why
    it can be useful to restart it several times.
    """

    def __init__(self, k=8, init='k-means++', n_init=10, max_iter=300,
                 tol=1e-4, verbose=0, random_state=None, copy_x=True):

        if hasattr(init, '__array__'):
            k = init.shape[0]

        self.k = k
        self.init = init
        self.max_iter = max_iter
        self.tol = tol
        self.n_init = n_init
        self.verbose = verbose
        self.random_state = random_state
        self.copy_x = copy_x

    def _check_data(self, X):
        """Verify that the number of samples given is larger than k"""
        if sp.issparse(X):
            raise ValueError("K-Means does not support sparse input matrices.")
        X = np.asanyarray(X)
        if X.shape[0] < self.k:
            raise ValueError("n_samples=%d should be >= k=%d" % (
                X.shape[0], self.k))
        return X

    def fit(self, X, y=None):
        """Compute k-means"""
        self.random_state = check_random_state(self.random_state)

        X = self._check_data(X)
        warn_if_not_float(X, self)

        self.cluster_centers_, self.labels_, self.inertia_ = k_means(
            X, k=self.k, init=self.init, n_init=self.n_init,
            max_iter=self.max_iter, verbose=self.verbose,
            tol=self.tol, random_state=self.random_state, copy_x=self.copy_x)
        return self

    def transform(self, X, y=None):
        """ Transform the data to a cluster-distance space

        In the new space, each dimension is the distance to the cluster centers.
        Note that even if X is sparse, the array returned by `transform` will
        typically be dense.

        Parameters
        ----------
        X: {array-like, sparse matrix}, shape = [n_samples, n_features]
            New data to transform.

        Returns
        -------
        X_new : array, shape [n_samples, k]
            X transformed in the new space.
        """
        if not hasattr(self, "cluster_centers_"):
            raise AttributeError("Model has not been trained. "
                                 "Train k-means before using transform.")
        cluster_shape = self.cluster_centers_.shape[1]
        if not X.shape[1] == cluster_shape:
            raise ValueError("Incorrect number of features for points. "
                             "Got %d features, expected %d" % (X.shape[1],
                                                               cluster_shape))
        return euclidean_distances(X, self.cluster_centers_)

    def predict(self, X):
        """Predict the closest cluster each sample in X belongs to.

        In the vector quantization literature, `cluster_centers_` is called
        the code book and each value returned by `predict` is the index of
        the closest code in the code book.

        Parameters
        ----------
        X: {array-like, sparse matrix}, shape = [n_samples, n_features]
            New data to predict.

        Returns
        -------
        Y : array, shape [n_samples,]
            Index of the closest center each sample belongs to.
        """
        if not hasattr(self, "cluster_centers_"):
            raise AttributeError("Model has not been trained yet. "
                                 "Fit k-means before using predict.")
        expected_n_features = self.cluster_centers_.shape[1]
        if not X.shape[1] == expected_n_features:
            raise ValueError("Incorrect number of features. "
                             "Got %d features, expected %d" % (
                                 X.shape[1], expected_n_features))
        return _e_step(X, self.cluster_centers_)[0]

def _mini_batch_step_dense(X, batch_slice, centers, counts, x_squared_norms):
    """Incremental update of the centers for the Minibatch K-Means algorithm

    Parameters
    ----------

    X: array, shape (n_samples, n_features)
        The original data array.

    batch_slice: slice
        The row slice of the mini batch.

    centers: array, shape (k, n_features)
        The cluster centers

    counts: array, shape (k, )
         The vector in which we keep track of the numbers of elements in a
         cluster

    x_squared_norms: array, shape (n_samples,)
        Squared euclidean norm of each data point.
    """
    # This is inefficient but saves mem and fits to sparse matrices.
    X = X[batch_slice]
    x_squared_norms = x_squared_norms[batch_slice]

    cache = euclidean_distances(centers, X, Y_norm_squared=x_squared_norms,
                                squared=True).argmin(axis=0)

    k = centers.shape[0]
    for q in range(k):
        center_mask = (cache == q)
        c = center_mask.sum()
        if np.any(center_mask):
            centers[q] = (1. / (counts[q] + c)) * (
                counts[q] * centers[q] + np.sum(X[center_mask], axis=0))
            counts[q] += c

def _mini_batch_step_sparse(X, batch_slice, centers, counts, x_squared_norms):
    """Incremental update of the centers for the Minibatch K-Means algorithm

    Parameters
    ----------

    X: csr_matrix, shape (n_samples, n_features)
        The data matrix in sparse CSR format.

    batch_slice: slice
        The row slice of the mini batch.

    centers: array, shape (k, n_features)
        The cluster centers

    counts: array, shape (k, )
         The vector in which we keep track of the numbers of elements in a
         cluster

    x_squared_norms: array, shape (n_samples,)
         The squared norms of each sample in `X`.
    """
    cache = euclidean_distances(centers, X[batch_slice],
              x_squared_norms[batch_slice]).argmin(axis=0).astype(np.int32)

    _k_means._mini_batch_update_sparse(X.data, X.indices, X.indptr,
                                       batch_slice, centers, counts, cache)

class MiniBatchKMeans(KMeans):
    """Mini-Batch K-Means clustering

    Parameters
    ----------

    k : int, optional, default: 8
        The number of clusters to form as well as the number of
        centroids to generate.

    max_iter : int
        Maximum number of iterations of the k-means algorithm for a
        single run.

    chunk_size: int, optional, default: 1000
        Size of the mini batches

    init : {'k-means++', 'random' or an ndarray}
        Method for initialization, defaults to 'random':

        'k-means++' : selects initial cluster centers for k-mean
        clustering in a smart way to speed up convergence. See section
        Notes in k_init for more details. Only for dense `X`.

        'random': choose k observations (rows) at random from data for
        the initial centroids.

        if init is an 2d array, it is used as a seed for the centroids

    tol: float, optional default: 1e-4
        Relative tolerance w.r.t. inertia to declare convergence

    Methods
    -------

    fit(X):
        Compute K-Means clustering

    partial_fit(X):
        Compute a partial K-Means clustering

    Attributes
    ----------

    cluster_centers_: array, [n_clusters, n_features]
        Coordinates of cluster centers

    labels_:
        Labels of each point

    inertia_: float
        The value of the inertia criterion associated with the chosen
        partition.

    References
    ----------
    http://www.eecs.tufts.edu/~dsculley/papers/fastkmeans.pdf
    """

    def __init__(self, k=8, init='random', max_iter=100,
                 chunk_size=1000, tol=1e-4, verbose=0, random_state=None):

        super(MiniBatchKMeans, self).__init__(k, init, 1,
              max_iter, tol, verbose, random_state)

        self.counts = None
        self.cluster_centers_ = None
        self.chunk_size = chunk_size

    def fit(self, X, y=None):
        """Compute the centroids on X by chunking it into mini-batches.

        Parameters
        ----------
        X: array-like, shape = [n_samples, n_features]
            Coordinates of the data points to cluster
        """
        self.random_state = check_random_state(self.random_state)
        X = check_arrays(X, sparse_format="csr", copy=False)[0]
        warn_if_not_float(X, self)
        n_samples, n_features = X.shape
        if n_samples < self.k:
            raise ValueError("Number of samples smaller than number "\
                             "of clusters.")

        if hasattr(self.init, '__array__'):
            self.init = np.asarray(self.init)

        X_shuffled = shuffle(X, random_state=self.random_state)

        if sp.issparse(X_shuffled):
            x_squared_norms = _k_means.csr_row_norm_l2(X)
        else:
            x_squared_norms = np.sum(X ** 2.0, axis=1)

        self.cluster_centers_ = _init_centroids(
            X_shuffled, self.k, self.init, random_state=self.random_state,
            x_squared_norms=x_squared_norms)
        self.counts = np.zeros(self.k, dtype=np.int32)

        n_batches = int(np.ceil(float(n_samples) / self.chunk_size))
        batch_slices = list(gen_even_slices(n_samples, n_batches))
        n_iterations = xrange(int(self.max_iter * n_batches))
        if sp.issparse(X_shuffled):
            _mini_batch_step = _mini_batch_step_sparse
            tol = self.tol
        else:
            _mini_batch_step = _mini_batch_step_dense
            tol = np.mean(np.var(X_shuffled, axis=0)) * self.tol

        for i, batch_slice in izip(n_iterations, cycle(batch_slices)):
            old_centers = self.cluster_centers_.copy()
            _mini_batch_step(X_shuffled, batch_slice, self.cluster_centers_,
                             self.counts, x_squared_norms=x_squared_norms)

            if np.sum((old_centers - self.cluster_centers_) ** 2) < tol:
                if self.verbose:
                    print 'Converged to similar centers at iteration', i
                break

        self.inertia_ = 0
        self.labels_ = np.empty((n_samples,), dtype=np.int)
        for batch_slice in batch_slices:
            batch_inertia, batch_labels = _calculate_labels_inertia(
            X[batch_slice], self.cluster_centers_)
            self.inertia_ += batch_inertia
            self.labels_[batch_slice] = batch_labels

        return self

    def partial_fit(self, X, y=None):
        """Update k means estimate on a single mini-batch X.

        Parameters
        ----------
        X: array-like, shape = [n_samples, n_features]
            Coordinates of the data points to cluster.
        """
        self.random_state = check_random_state(self.random_state)

        X = check_arrays(X, sparse_format="csr", copy=False)[0]
        n_samples, n_features = X.shape
        if hasattr(self.init, '__array__'):
            self.init = np.asarray(self.init)

        if n_samples == 0:
            return self

        if sp.issparse(X):
            x_squared_norms = _k_means.csr_row_norm_l2(X)
        else:
            x_squared_norms = (X ** 2).sum(axis=1)

        if self.counts is None:
            # this is the first call partial_fit on this object:
            # initialize the cluster centers
            self.cluster_centers_ = _init_centroids(
                X, self.k, self.init, random_state=self.random_state,
                x_squared_norms=x_squared_norms)

            self.counts = np.zeros(self.k, dtype=np.int32)

        batch_slice = slice(0, n_samples, None)
        if sp.issparse(X):
            _mini_batch_step = _mini_batch_step_sparse
        else:
            _mini_batch_step = _mini_batch_step_dense

        _mini_batch_step(X, batch_slice, self.cluster_centers_, self.counts,
                         x_squared_norms=x_squared_norms)

        self.inertia_, self.labels_ = _calculate_labels_inertia(
            X, self.cluster_centers_, x_squared_norms)

        return self