text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Assets.Scripts.Game.Components
{
    public class TimeController : MonoBehaviour
    {
        public void ToggleTime()
        {
            if (Time.timeScale == 0)
            {
                Time.timeScale = 1;
            }
            else
            {
                Time.timeScale = 0;
            }
        }
    }
}


text:
"use strict";

/*jshint unused: false*/ // deactivating this, as we define abstract interfaces here

var _ = require('underscore');
var util = require('substance-util');
var errors = util.errors;

errors.define("ChronicleError", -1);
errors.define("ChangeError", -1);

// A change recorded in the chronicle
// ========
//
// Each change has an unique id (equivalent to git SHA).
// A change can have multiple parents (merge).
//
// options:
//   - id: a custom id for the change

var Change = function(id, parent, data) {

  this.type = 'change';

  if (!id) {
    throw new errors.ChangeError("Every change needs a unique id.");
  }
  this.id = id;

  if (!parent) {
    throw new errors.ChangeError("Every change needs a parent.");
  }

  this.parent = parent;

  // Application specific data
  // --------
  //
  // This needs to contain all information to be able to apply and revert
  // a change.

  this.data = data;

  this.uuid = util.uuid;

};

Change.prototype = {

  toJSON: function() {
    return {
      type: this.type,
      id: this.id,
      parent: this.parent,
      data: this.data
    };
  }

};

Change.fromJSON = function(json) {
  if (json.type === Merge.TYPE) return new Merge(json);
  if (json.type === Transformed.TYPE) return new Transformed(json);

  return new Change(json.parent, json.data, json);
};

// a dedicated global root node
var ROOT = "ROOT";
var ROOT_NODE = new Change(ROOT, true, null);
ROOT_NODE.parent = ROOT;

// A dedicated Change for merging multiple Chronicle histories.
// ========
//
// A merge is described by a command containing a diff for each of the parents (see Index.diff()).
//
// Example: Consider two sequences of changes [c0, c11, c12] and [c0, c21, c22, c23].
//
//  A merge taking all commits of the second ('theirs') branch and
//  rejecting those of the first ('mine') would be:
//
//    merge = {
//      "c12": ["-", "c11", "c0" "+", "c21", "c22", "c23"],
//      "c23": []
//    }
//
// A manually selected merge with [c11, c21, c23] would look like:
//
//    merge = {
//      "c12": ["-", "c11", "+", "c21", "c23"],
//      "c23": ["-", "c22", "c21", "c0", "+", "c11", "c21", "c23"]
//    }
//

var Merge = function(id, main, branches) {
  Change.call(this, id, main);
  this.type = Merge.TYPE;

  if (!branches) {
    throw new errors.ChangeError("Missing branches.");
  }
  this.branches = branches;
};

Merge.Prototype = function() {

  var __super__ = util.prototype(this);

  this.toJSON = function() {
    var result = __super__.toJSON.call(this);
    result.type = Merge.TYPE;
    result.branches = this.branches;
    return result;
  };

};
Merge.Prototype.prototype = Change.prototype;
Merge.prototype = new Merge.Prototype();

Merge.TYPE =  "merge";

Merge.fromJSON = function(data) {
  if (data.type !== Merge.TYPE) throw new errors.ChangeError("Illegal data for deserializing a Merge node.");
  return new Merge(data.parent, data.branches, data);
};

// Transformed changes are those which have been
// created by transforming (rebasing) another existing change.
// For the time being, the data is persisted redundantly.
// To be able to track the original source of the change,
// this type is introduced.
var Transformed = function(id, parent, data, original) {
  Change.call(this, id, parent, data);
  this.type = Transformed.TYPE;
  this.original = original;
};

Transformed.Prototype = function() {

  var __super__ = util.prototype(this);

  this.toJSON = function() {
    var result = __super__.toJSON.call(this);
    result.type = Transformed.TYPE;
    result.original = this.original;
    return result;
  };

};

Transformed.TYPE = "transformed";

Transformed.fromJSON = function(json) {
  if (json.type !== Transformed.TYPE) throw new errors.ChangeError("Illegal data for deserializing a Transformed node.");
  return new Transformed(json.parent, json.data, json.original, json);
};

Transformed.Prototype.prototype = Change.prototype;
Transformed.prototype = new Transformed.Prototype();

// A class that describes the difference of two states by
// a sequence of changes (reverts and applies).
// =======
//
// The difference is a sequence of commands that forms a transition from
// one state to another.
//
// A diff is specified using the following syntax:
//    [- sha [shas ...]] [+ sha [shas ...]]
// where '-' preceeds a sequence reverts and '+' a sequence of applies.
// Any diff can be described in that order (reverts followed by applies)
//
// Example: Consider an index containing the following changes
//
//        , - c11 - c12
//      c0
//        ` - c21 - c22 - c23
//
// Diffs for possible transitions look like:
// "c21" -> "c23" : ["+", "c22", "c23"]
// "c12" -> "c0" :  ["-", "c11", "c0" ]
// "c21" -> "c11" : ["-", "c0", "+", "c11"]

var Diff = function() {};

Diff.prototype = {

  hasReverts: function() {
    throw new errors.SubstanceError("Not implemented.");
  },

  // Provides the changes that will be reverted
  // --------

  reverts: function() {
    throw new errors.SubstanceError("Not implemented.");
  },

  hasApplies: function() {
    throw new errors.SubstanceError("Not implemented.");
  },

  // Provides the changes that will applied
  // --------

  applies: function() {
    throw new errors.SubstanceError("Not implemented.");
  },

  // Provides the sequence of states visited by this diff.
  // --------

  sequence: function() {
    throw new errors.SubstanceError("Not implemented.");
  },

  // Provides the path from the root to the first change
  // --------
  //
  // The naming refers to a typical diff situation where
  // two branches are compared. The first branch containing the own
  // changes, the second one the others.

  mine: function() {
    throw new errors.SubstanceError("Not implemented.");
  },

  // Provides the path from the root to the second change
  // --------
  //

  theirs: function() {
    throw new errors.SubstanceError("Not implemented.");
  },

  // Provides the common root of the compared branches.
  // --------
  //

  root: function() {
    throw new errors.SubstanceError("Not implemented.");
  },

  // Provides the version this diff has to be applied on.
  // --------

  start: function() {
    throw new errors.SubstanceError("Not implemented.");
  },

  // Provides the version which is generated by applying this diff.
  // --------

  end: function() {
    throw new errors.SubstanceError("Not implemented.");
  },

  // Provides a copy that represents the inversion of this diff.
  // --------

  inverted: function() {
    throw new errors.SubstanceError("Not implemented.");
  },

};

// Creates a new diff for the given reverts and applies
// --------
// Note this factory is provided when loading index_impl.js

Diff.create = function(reverts, applies) {
  /*jshint unused: false*/
  throw new errors.SubstanceError("Not implemented.");
};

// A Chronicle contains the history of a versioned object.
// ========
//

var Chronicle = function(index, options) {
  options = options || {};

  // an instance implementing the 'Index' interface
  this.index = index;

  // the versioned object which must implement the 'Versioned' interface.
  this.versioned = null;

  // flags to control the chronicle's behaviour
  this.__mode__ = options.mode || Chronicle.DEFAULT_MODE;
};

Chronicle.Prototype = function() {

  // Records a change
  // --------
  //
  // Creates a commit and inserts it into the index at the current position.
  //
  // An application should call this after having applied the change to the model successfully.
  // The provided 'change' should contain every information that is necessary to
  // apply the change in both directions (apply and revert).
  //
  // Note: this corresponds to a 'git commit' in git.

  this.record = function(change) {
    throw new errors.SubstanceError("Not implemented.");
  };

  // Opens a specific version.
  // --------
  //
  // Brings the versioned object as well as the index to the state
  // of the given state.
  //

  this.open = function(version) {
    throw new errors.SubstanceError("Not implemented.");
  };

  // Performs an incremental transformation.
  // --------
  //
  // The given state must be a direct neighbor of the current state.
  // For convenience a sequence of consecutive states can be given.
  //
  // Call this if you already know path between two states
  // or if you want to apply or revert a single change.
  //
  // Returns the change applied by the step.
  //

  this.step = function(next) {
    throw new errors.SubstanceError("Not implemented.");
  };

  this.forward = function(toward) {
    var state = this.versioned.getState();
    if (state === toward) return;

    var children = this.index.children[state];

    if (children.length === 0) return;

    var next;

    if (children.length === 1) {
      next = children[0];
    }
    else if (toward) {
      var path = this.index.shortestPath(state, toward);
      path.shift();
      next = path.shift();
    }
    else {
      next = children[children.length-1];
    }

    if (next) {
      return this.step(next);
    } else {
      return;
    }
  };

  this.rewind = function() {
    var current = this.index.get(this.versioned.getState());
    var previous;
    if (current.id === ROOT) return null;

    previous = current.parent;
    return this.step(previous);
  };

  // Create a commit that merges a history specified by its last commit.
  // --------
  //
  // The strategy specifies how the merge should be generated.
  //
  //  'mine':   reject the changes of the other branch
  //  'theirs': reject the changes of this branch
  //  'manual': compute a merge that leads to the given sequence.
  //
  // Returns the id of the new state.
  //

  this.merge = function(state, strategy, sequence) {
    throw new errors.SubstanceError("Not implemented.");
  };

  // Making this instance the chronicler of the given Versioned instance.
  // --------
  //

  this.manage = function(versioned) {
    this.versioned = versioned;
  };

  // Marks the current version.
  // --------
  //

  this.mark = function(name) {
    this.index.setRef(name, this.versioned.getState());
  };

  // Provides the id of a previously marked version.
  // --------
  //

  this.find = function(name) {
    return this.index.getRef(name);
  };

  // Get the current version.
  // --------
  //

  this.getState = function() {
    return this.versioned.getState();
  };

  // Retrieve changes.
  // --------
  //
  // If no range is given a full path is returned.

  this.getChanges = function(start, end) {
    var changes = [];
    var path = this.path(start, end);

    _.each(path, function(id) {
      changes.push(this.index.get(id));
    }, this);

    return changes;
  };

  this.canRedo = function() {
    var state = this.versioned.getState();
    var children = this.index.children[state];
    return children.length > 0;
  };

  this.canUndo = function() {
    var root = this.index.get(ROOT);
    var current = this.index.get(this.versioned.getState());
    return (current !== root);
  };

};

Chronicle.prototype = new Chronicle.Prototype();

// only allow changes that have been checked via instant apply+revert
Chronicle.PEDANTIC_RECORD = 1 << 1;

// performs a reset for all imported changes
Chronicle.PEDANTIC_IMPORT = 1 << 2;

Chronicle.HYSTERICAL = Chronicle.PEDANTIC_RECORD | Chronicle.PEDANTIC_IMPORT;
Chronicle.DEFAULT_MODE = Chronicle.PEDANTIC_IMPORT;

// The factory method to create a Chronicle instance
// --------
// options:
//  store: a Substance Store used to persist the index
Chronicle.create = function(options) {
  throw new errors.SubstanceError("Not implemented.");
};

// A directed acyclic graph of Commit instances.
// ========
//
var Index = function() {
  this.__id__ = util.uuid();

  this.changes = {};
  this.refs = {};
  this.children = {};
  this.changes[ROOT] = ROOT_NODE;
  this.children[ROOT] = [];
};

Index.Prototype = function() {

  // Adds a change to the index.
  // --------
  // All parents must be registered first, otherwise throws an error.
  //

  this.add = function(change) {
    throw new errors.SubstanceError("Not implemented.");
  };

  // Removes a change from the index
  // --------
  // All children must be removed first, otherwise throws an error.
  //

  this.remove = function(id) {
    throw new errors.SubstanceError("Not implemented.");
  };

  // Checks if a given changeId has been added to the index.
  // --------
  //

  this.contains = function(changeId) {
    throw new errors.SubstanceError("Not implemented.");
  };

  // Retrieves a (shortest) path between two versions
  // --------
  //
  // If no end change is given it returns the path starting
  // from ROOT to the start change.
  // path() returns the path from ROOT to the current state.
  //

  this.path = function(start, end) {
    throw new errors.SubstanceError("Not implemented.");
  };

  // Retrieves a change by id
  // --------
  //

  this.get = function(id) {
    throw new errors.SubstanceError("Not implemented.");
  };

  // Provides all changes that are direct successors of this change.
  // --------
  //

  this.getChildren = function(id) {
    throw new errors.SubstanceError("Not implemented.");
  };

  // Lists the ids of all contained changes
  // --------
  //

  this.list = function() {
    throw new errors.SubstanceError("Not implemented.");
  };

  // Computes the difference betweend two changes
  // --------
  //
  // In contrast to `path` is a diff a special path that consists
  // of a sequence of reverts followed by a sequence of applies.
  //

  this.diff = function(start, end) {
    throw new errors.SubstanceError("Not implemented.");
  };

  // Sets a reference to look up a change via name.
  // ---------
  //

  this.setRef = function(name, id) {
    if (this.changes[id] === undefined) {
      throw new errors.ChronicleError("Unknown change: " + id);
    }
    this.refs[name] = id;
  };

  // Looks-up a change via name.
  // ---------
  //

  this.getRef = function(name) {
    return this.refs[name];
  };

  this.listRefs = function() {
    return Object.keys(this.refs);
  };

  // Imports all commits from another index
  // --------
  //
  // Note: this corresponds to a 'git fetch', which only adds commits without
  // applying any changes.
  //

  this.import = function(otherIndex) {
    throw new errors.SubstanceError("Not implemented.");
  };

};

Index.prototype = new Index.Prototype();

Index.INVALID = "INVALID";
Index.ROOT = ROOT_NODE;

Index.create = function() {
  throw new errors.SubstanceError("Not implemented.");
};

// Creates an adapter for Changes given as plain hash.
// The adapter can be used together with Index.import
Index.adapt = function(changes) {
  return {
    list: function() {
      return _.keys(changes);
    },
    get: function(id) {
      return changes[id];
    }
  };
};

// A interface that must be implemented by objects that should be versioned.
var Versioned = function(chronicle) {
  this.chronicle = chronicle;
  this.state = ROOT;
  chronicle.manage(this);
};

Versioned.Prototype = function() {

  // Applies the given change.
  // --------
  //

  this.apply = function(change) {
    throw new errors.SubstanceError("Not implemented.");
  };

  // Reverts the given change.
  // --------
  //

  this.revert = function(change) {
    change = this.invert(change);
    this.apply(change);
  };

  // Inverts a given change
  // --------
  //

  this.invert = function(change) {
    throw new errors.SubstanceError("Not implemented.");
  };

  // Transforms two sibling changes.
  // --------
  //
  // This is the `transform` operator provided by Operational Transformation.
  //
  //       / - a            / - a - b' \
  //      o          ~ >   o             p
  //       \ - b            \ - b - a' /
  //
  // I.e., the result of applying `a - b'` must lead to the same result as
  // applying `b - a'`.
  //
  // options:
  //
  //  - check:    enables conflict checking. A MergeConflict is thrown as an error
  //              when a conflict is found during transformation.
  //  - inplace:  transforms the given instances a and b directly, without copying.
  //
  // returns: [a', b']

  this.transform = function(a, b, options) {
    throw new errors.SubstanceError("Not implemented.");
  };

  // Provides the current state.
  // --------
  //

  this.getState = function() {
    return this.state;
  };

  // Sets the state.
  // --------
  //
  // Note: this is necessary for implementing merges.
  //

  this.setState = function(state) {
    this.state = state;
  };

  // Resets the versioned object to a clean state.
  // --------
  //

  this.reset = function() {
    this.state = ROOT;
  };
};

Versioned.prototype = new Versioned.Prototype();

Chronicle.Change = Change;
Chronicle.Merge = Merge;
Chronicle.Transformed = Transformed;
Chronicle.Diff = Diff;
Chronicle.Index = Index;
Chronicle.Versioned = Versioned;
Chronicle.ROOT = ROOT;

Chronicle.mergeConflict = function(a, b) {
  var conflict = new errors.MergeConflict("Merge conflict: " + JSON.stringify(a) +" vs " + JSON.stringify(b));
  conflict.a = a;
  conflict.b = b;
  return conflict;
};

module.exports = Chronicle;

redpajama_set_name: RedPajamaGithub


text:
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using StackExchange.Redis;

namespace StackExchange.Opserver.Data.Redis
{
    public partial class RedisInstance
    {
        /// <summary>
        /// Slave this instance to another instance
        /// </summary>
        public async Task<bool> SlaveToAsync(string address)
        {
            var newMaster = EndPointCollection.TryParse(address);
            await _connection.GetSingleServer().SlaveOfAsync(newMaster);
            var newMasterInstance = GetInstance(address);
            await newMasterInstance?.PublishSERedisReconfigureAsync();
            return true;
        }

        /// <summary>
        /// Promote this instance to a master
        /// </summary>
        public string PromoteToMaster()
        {
            using (var log = new StringWriter())
            {
                _connection.GetSingleServer().MakeMaster(ReplicationChangeOptions.Broadcast, log);
                return log.ToString();
            }
        }

        /// <summary>
        /// The StackExchange.Redis tiebreaker key this node is currently using.
        /// </summary>
        /// <remarks>If this doesn't match the key (likely default) used in the other clients, it will have little or no effect</remarks>
        public string SERedisTiebreakerKey => ConfigurationOptions.Parse(_connection.Configuration).TieBreaker;

        /// <summary>
        /// Sets the StackExchange.Redis tiebreaker key on this node.
        /// </summary>
        public async Task<bool> SetSERedisTiebreakerAsync()
        {
            RedisKey tieBreakerKey = SERedisTiebreakerKey;

            var myEndPoint = _connection.GetEndPoints().FirstOrDefault();
            RedisValue tieBreakerValue = EndPointCollection.ToString(myEndPoint);

            var result = await _connection.GetDatabase()
                .StringSetAsync(tieBreakerKey, tieBreakerValue, flags: CommandFlags.NoRedirect | CommandFlags.HighPriority);
            Tiebreaker.Poll(true);
            return result;
        }

        /// <summary>
        /// Gets the current value of the StackExchange.Redis tiebreaker key on this node.
        /// </summary>
        public Task<string> GetSERedisTiebreakerAsync()
        {
            return GetSERedisTiebreakerAsync(_connection);
        }

        private async Task<string> GetSERedisTiebreakerAsync(ConnectionMultiplexer conn)
        {
            RedisKey tieBreakerKey = ConfigurationOptions.Parse(conn.Configuration).TieBreaker;
            return await conn.GetDatabase().StringGetAsync(tieBreakerKey, CommandFlags.NoRedirect);
        }

        /// <summary>
        /// Clears the StackExchange.Redis tiebreaker key from this node.
        /// </summary>
        public async Task<bool> ClearSERedisTiebreakerAsync()
        {
            RedisKey tieBreakerKey = SERedisTiebreakerKey;
            var result = await _connection.GetDatabase()
                .KeyDeleteAsync(tieBreakerKey, flags: CommandFlags.NoRedirect | CommandFlags.HighPriority);
            Tiebreaker.Poll(true);
            return result;
        }

        /// <summary>
        /// Instructs the redis node to broadcast a reconfiguration request to all StackExchange.Redis clients.
        /// </summary>
        public Task<long> PublishSERedisReconfigureAsync()
        {
            return _connection.PublishReconfigureAsync();
        }

        /// <summary>
        /// Kill a particular client's connection
        /// </summary>
        public async Task<bool> KillClientAsync(string address)
        {
            var endpoint = EndPointCollection.TryParse(address);
            if (endpoint == null) return false;
            await _connection.GetSingleServer().ClientKillAsync(endpoint);
            return true;
        }

        /// <summary>
        /// List of targets this instance may be slaved to, does not include:
        ///  - Current Master
        ///  - Any slaves in the chain (circles bad, k?)
        ///  - Itself
        /// </summary>
        public List<RedisInstance> RecommendedMasterTargets
        {
            get
            {
                return AllInstances.Where(s => s.Port == Port && s.Host != Host && !GetAllSlavesInChain().Contains(s) && Master != s).ToList();
            }
        }
    }
}


text:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.DirectoryServices.AccountManagement;
using Xunit;

namespace AccountManagementUnitTests
{
    ///
    ///This is a test class for UserPrincipalTest and is intended
    ///to contain all UserPrincipalTest Unit Tests
    ///
    public class UserPrincipalTest : PrincipalTest
    {
        #region Additional test attributes
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        internal override Principal CreatePrincipal(PrincipalContext context, string name)
        {
            UserPrincipal user = new UserPrincipal(context);
            user.Name = name;
            return user;
        }

        internal override Principal CreateExtendedPrincipal(PrincipalContext context, string name)
        {
            ExtendedUserPrincipal user = new ExtendedUserPrincipal(context);
            user.Name = name;
            return user;
        }

        internal override Principal FindExtendedPrincipal(PrincipalContext context, string name)
        {
            return ExtendedUserPrincipal.FindByIdentity(context, name);
        }

        ///
        ///A test for UserPrincipal Constructor
        ///
        public void UserPrincipalConstructorTest()
        {
            UserPrincipal user = new UserPrincipal(domainContext);
            user.Dispose();
            //Assert.Inconclusive("TODO: Implement code to verify target");
        }

        public void ComputedUACCheck()
        {
            string username = "Administrator";
            string password = "Adrumble@6";
            //TODO: don't assume it exists, create it if its not
            string OU = "TestNull";
            string baseDomain = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split(new char[] { '\\' })[1] + "-TEST";
            string domain = System.String.Format("{0}.nttest.microsoft.com", baseDomain);
            string container = System.String.Format("ou={0},dc={1},dc=nttest,dc=microsoft,dc=com", OU, baseDomain);

            PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, container, username, password);
            UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "good");

            // set the wrong password to force account lockout
            // Is there a way of doing it programmatically except for NetUserSetInfo? (managed code)
            context.ValidateCredentials("good", "wrong password");

            //verify that the account is locked out
            Assert.True(user.IsAccountLockedOut(), "trying wrong credentials did not lock the account");

            // if uac is not set correctly, this call might clear the lockout
            user.SmartcardLogonRequired = false;
            user.Save();

            //verify that the account is still locked out
            Assert.True(user.IsAccountLockedOut(), "the account is no longer locked out after writing setting SmartCardLogonRequired");
        }
    }
}


text:
package examen.preparacion.biblioteca;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private LibrosSQLiteHelper librosSQLiteHelper;
    private ListView list_view_principal;
    private final String NOMBRE = "Biblioteca";

    private List<Libro> lista_libros;
    private static LibroAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list_view_principal = (ListView) findViewById(R.id.list_view_principal);
        librosSQLiteHelper = new LibrosSQLiteHelper(getApplicationContext(), NOMBRE, null, 1); //Inicio de la BDD por primera vez.

        lista_libros = librosSQLiteHelper.cogerDatos();
        adapter = new LibroAdapter(this, lista_libros);

        list_view_principal.setAdapter(adapter);

        Collections.sort(lista_libros, new Comparator<Libro>() {
            @Override
            public int compare(Libro uno, Libro dos) {
                return uno.getTitulo().compareToIgnoreCase(dos.getTitulo());
            }
        });

        list_view_principal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Libro libro = (Libro) adapterView.getItemAtPosition(position);

                Intent intent = new Intent(getApplicationContext(), AltaLibro.class);
                intent.putExtra("_id", libro.getId());
                intent.putExtra("categoria", libro.getCategoria());
                intent.putExtra("titulo", libro.getTitulo());
                intent.putExtra("autor", libro.getAutor());
                intent.putExtra("idioma", libro.getIdioma());
                intent.putExtra("fechainicio", libro.getFechaInicio());
                intent.putExtra("fechafin", libro.getFechaFin());
                intent.putExtra("prestado", libro.getPrestado());
                intent.putExtra("valoracion", libro.getValoracion());
                intent.putExtra("formato", libro.getFormato());
                intent.putExtra("notas", libro.getNotas());

                startActivity(intent);
            }
        });

        list_view_principal.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
                final Libro libro = (Libro) adapterView.getItemAtPosition(position);
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("Eliminar")
                        .setMessage("¿Eliminar?")
                        .setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                librosSQLiteHelper.delete(libro.getId());
                                adapter.remove(libro);
                                adapter.notifyDataSetChanged();
                                Toast.makeText(MainActivity.this, "Delete Hecho", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(MainActivity.this, "Cancelado", Toast.LENGTH_SHORT).show();
                            }
                        }).show();
                return true;
            }
        });

        //int count = 0;
        //if(count++ == 0) librosSQLiteHelper.insert("Literatura", "El Quijote", "Cervantes", "Español", "Ebook", "12/01/1998", "12/12/2002", "Antonio", 3, "Un Clasico");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private void ordenar() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Ordenar Por...");
        String[] types = {"Titulo", "Autor","Idioma","Categoria","Formato","Valoracion"};
        builder.setItems(types, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int elemento) {
                switch(elemento){
                    case 0:
                        Collections.sort(lista_libros, new Comparator<Libro>() {
                            @Override
                            public int compare(Libro uno, Libro dos) {
                                return uno.getTitulo().compareToIgnoreCase(dos.getTitulo());
                            }
                        });
                        adapter.notifyDataSetChanged();
                        break;
                    case 1:
                        Collections.sort(lista_libros, new Comparator<Libro>() {
                            @Override
                            public int compare(Libro uno, Libro dos) {
                                return uno.getAutor().compareToIgnoreCase(dos.getAutor());
                            }
                        });
                        adapter.notifyDataSetChanged();
                        break;
                    case 2:
                        Collections.sort(lista_libros, new Comparator<Libro>() {
                            @Override
                            public int compare(Libro uno, Libro dos) {
                                return uno.getIdioma().compareToIgnoreCase(dos.getIdioma());

                            }
                        });
                        adapter.notifyDataSetChanged();
                        break;

                    case 3:
                        Collections.sort(lista_libros, new Comparator<Libro>() {
                            @Override
                            public int compare(Libro uno, Libro dos) {
                                return uno.getCategoria().compareToIgnoreCase(dos.getCategoria());

                            }
                        });
                        adapter.notifyDataSetChanged();
                        break;
                    case 4:
                        Collections.sort(lista_libros, new Comparator<Libro>() {
                            @Override
                            public int compare(Libro uno, Libro dos) {
                                return uno.getFormato().compareToIgnoreCase(dos.getFormato());
                            }
                        });
                        adapter.notifyDataSetChanged();
                        break;
                    case 5:
                        Collections.sort(lista_libros, new Comparator<Libro>() {
                            @Override
                            public int compare(Libro uno, Libro dos) {
                                return Float.compare(dos.getValoracion(),uno.getValoracion());
                            }
                        });
                        adapter.notifyDataSetChanged();
                        break;
                }
                dialog.dismiss();
            }
        }).show();
    }

     @Override
    public boolean onOptionsItemSelected(MenuItem item) {
         int id = item.getItemId();

         switch(id) {
             case(R.id.altalibro):
                 Intent intent = new Intent(MainActivity.this, AltaLibro.class);
                 startActivity(intent);
                 break;
             case(R.id.ordenarpor):
                 ordenar();
                 break;
             case(R.id.exportar):
                 exportar();
                 break;
             case(R.id.importar):
                 importar();
                 break;
             case(R.id.acercade):
                 new AcercaDe().show(getFragmentManager(), "Acerca De...");
         }

         return true;
     }

    @Override
    public void onResume() {
        super.onResume();
        librosSQLiteHelper = new LibrosSQLiteHelper(getApplicationContext(), NOMBRE, null, 1); //Inicio de la BDD por primera vez.

        lista_libros = librosSQLiteHelper.cogerDatos();
        adapter = new LibroAdapter(this, lista_libros);

        list_view_principal.setAdapter(adapter);

        Collections.sort(lista_libros, new Comparator<Libro>() {
            @Override
            public int compare(Libro uno, Libro dos) {
                return uno.getTitulo().compareToIgnoreCase(dos.getTitulo());
            }
        });
    }

    private void importar() {
        try {
            File almacenamiento = Environment.getExternalStorageDirectory();
            File datos = Environment.getDataDirectory();
            String currentDBPath = "//data//"+getPackageName()+"//databases//"+NOMBRE;
            String backupDBPath = "backup.db";
            File actual = new File(datos, currentDBPath);
            File copia = new File(almacenamiento, backupDBPath);
            if (actual.exists()) {
                FileChannel fis = new FileInputStream(copia).getChannel();
                FileChannel fos = new FileOutputStream(actual).getChannel();
                fos.transferFrom(fis, 0, fis.size());
                fis.close();
                fos.close();
                Toast.makeText(getApplicationContext(), "Importado", Toast.LENGTH_SHORT).show();

            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        onResume();
    }

    private void exportar() {
        try {
            File almacenamiento = Environment.getExternalStorageDirectory();
            File datos = Environment.getDataDirectory();
            String currentDBPath = "//data//"+getPackageName()+"//databases//"+NOMBRE;
            String backupDBPath = "backup.db";
            File actual = new File(datos, currentDBPath);
            File copia = new File(almacenamiento, backupDBPath);
            if (actual.exists()) {
                FileChannel fis = new FileInputStream(actual).getChannel();
                FileChannel fos = new FileOutputStream(copia).getChannel();
                fos.transferFrom(fis, 0, fis.size());
                fis.close();
                fos.close();
                Toast.makeText(getApplicationContext(),"Exportado",Toast.LENGTH_SHORT).show();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}